我的程序在下面反转一个字符串,而不是通过字符而不是字.
示例输入 Hello Mello Sanple输出 Hello Mello Hello
我不知道我从哪里得到第一个Hello.
#include "stdafx.h"
#include <iostream>
int main(int argc, char **argv)
{
char input[255];
char *head; // head of a word.
char *tail; // tail of a word.
printf("Enter a sentence: ");
gets_s(input); // read a whole line.
tail = input + strlen(input) - 1;
while (tail >= input)
{
if (*tail == 0 || *tail == ' ')
{
tail--; // move to the tail of a word.
}
else
{
tail[1] …Run Code Online (Sandbox Code Playgroud) 我是C++的新手.我使用Visual Studio.我的调试版本按预期工作,但发布版本因访问冲突而崩溃.结构:
struct GeneticData
{
int NumberOfIdDataPairs;
char** IdBuffer;
char** DataBuffer;
};
Run Code Online (Sandbox Code Playgroud)
IdBuffer是一个由20个字符组成的char数组,Databuffer是一个由102个字符组成的字符数组.返回指针的函数:
GeneticData* LoadData(FILE* dataFilePtr) //dataFilePtr is valid in the function
{
char* result = NULL;
int fileLine = 0;
GeneticData geneticData = *new GeneticData();
geneticData.NumberOfIdDataPairs = *new int;
geneticData.IdBuffer = new char*[gSizeOfBuffer];
geneticData.DataBuffer = new char*[gSizeOfBuffer];
for (int i = 0; i < gSizeOfBuffer; i++)
{
geneticData.IdBuffer[i] = new char[gSizeOfId];
geneticData.DataBuffer[i] = new char[gSizeOfData];
}
do
{
result = fgets(geneticData.IdBuffer[fileLine], gSizeOfId, dataFilePtr);
fgets(geneticData.DataBuffer[fileLine], gSizeOfData, dataFilePtr);
fileLine++;
} while …Run Code Online (Sandbox Code Playgroud) 我有一个字符串数组,当它不再有NULL指针(意味着数组已满)时,我想要它.我已经尝试了realloc并没有成功,我想我并没有正确地指向思考.
这是我的代码:
int storage; //global, outside of main
int i, key;
char **people;
char **phones;
printf("Please enter a storage cacpity:\n");
scanf("%d",&storage);
printf("\n");
people=malloc(storage*sizeof(char *));
phones=malloc(storage*sizeof(char *));
for (i=0; i<storage; i++) {
people[i] = NULL;
phones[i] = NULL;
}
void AddNewContact(char * people[], char * phones[]) {
char name[100];
char phone[12];
int i, listfull = 0;
printf("Enter a contact name:\n");
scanf("%s",&name);
printf("Enter a phone number:\n");
scanf("%s",&phone);
for (i=0; i<storage; i++) {
if (people[i]==NULL) {
people[i] = (char *)malloc(strlen(name));
phones[i] = (char …Run Code Online (Sandbox Code Playgroud) 即使我在C++中为类重新定义了析构函数,是否可以调用默认的析构函数?例如,如果我有两个班级
class B{
...
};
class A{
private:
B* p;
public:
A(B b):p(new B(b)){...}
...
~A(){delete p;}
};
Run Code Online (Sandbox Code Playgroud)
我并不总是想删除p指向的对象.
我有两个班级说A&B是这样的:
// A.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef A_H_
#define A_H_
#include "B.h"
class A {
public:
std::vector<B> bVec;
A();
void foo();
virtual ~A();
};
#endif /* A_H_ */
//--------------------
// A.cpp
#include "A.h"
A::A() {
B b(this);
bVec.push_back(b);
}
void A::foo() {
for(int i=0; i<bVec.size(); i++)
bVec[i].addNewB();
}
A::~A() {
}
//--------------------
// B.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef B_H_
#define B_H_
class A;
class B {
public:
A* parent;
double data[15];
B(A* …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++中的类来玩.来自Java世界,它们略有不同.
我要做的事情应该是显而易见的:我有一个名为的类SomeClass,它只有一个int.然后我有一个名为的类A,其中
// Create a class holding just an integer.
class SomeClass {
public:
int _value;
SomeClass(int value) {
this->_value = value;
}
};
class A {
private:
// The property _someClassPointer holds a pointer to a SomeClass object.
SomeClass *_someClassPointer;
public:
// We can set a SomeClass instance as a property of the A class.
void setSomeClass(SomeClass *someClassPointer) {
// It copies the local pointer value to the property.
this->_someClassPointer = someClassPointer;
} …Run Code Online (Sandbox Code Playgroud) 我正在编写一个C++函数,它应该通过将每个元素逐个字符复制到一个新数组来复制一个字符数组.理想情况下,如果我发表声明
char* a = "test";
char* b = copyString(a);
Run Code Online (Sandbox Code Playgroud)
那么a和b都应该包含字符串"test".但是,当我打印复制的数组b时,我得到"test"加上一系列似乎是指针的无意义字符.我不想要那些,但我无法弄清楚我哪里出错了.
我目前的功能如下:
char* copyString(char* s)
{
//Find the length of the array.
int n = stringLength(s);
//The stringLength function simply calculates the length of
//the char* array parameter.
//For each character that is not '\0', copy it into a new array.
char* duplicate = new char[n];
for (int j = 0; j < n; j++)
{
duplicate[j] = s[j];
//Optional print statement for debugging.
cout << duplicate[j] << endl;
}
//Return …Run Code Online (Sandbox Code Playgroud) 这是一个C问题:
我不明白为什么这段代码有效:
char *c[] = {"hello","world"};
Run Code Online (Sandbox Code Playgroud)
但这并不是:
int *v[] = {{1,2},{3,4}};
Run Code Online (Sandbox Code Playgroud)
对我来说,它们是相同的东西(用各自类型初始化的指针数组)但显然它们不是.什么是exaclty那么差异呢?感谢名单.
编辑:如果对我的帖子投降的人可以说为什么这是一个糟糕的问题...这将是伟大的.
当我尝试在函数内部打印成员变量时,它会给出我想要的结果.但是,如果我返回此成员变量然后尝试在main中访问它,它会给我一个不同的结果.为什么会这样?
这是我的代码的样子:
Node.h:
#include <cstddef>
#include <vector>
#include <iostream>
using namespace std;
class Node{
public:
int v;
Node * parent;
Node(int I);
Node(int I,Node * p);
vector<Node*> myfun();
}
Run Code Online (Sandbox Code Playgroud)
Node.cpp:
Node::Node(int I){
v = I;
parent = NULL;
}
Node::Node(int I,Node * p){
v = I;
parent = p;
}
vector<Node*> Node::myfun(){
vector<Node*> myvec;
Node next1(1,this);
myvec.push_back(&next1);
Node next2(2,this);
myvec.push_back(&next2);
cout << myvec[0]->v << endl; // prints out "1"
cout << myvec[1]->v << endl; // prints out "2"
return(myvec);
} …Run Code Online (Sandbox Code Playgroud) 我想将一个char指针作为参数传递给这样的函数:
void foo(char* array_of_c_str[], const int size_of_array, char* result_ptr)
{
// my logic
result_ptr = a[n];
}
Run Code Online (Sandbox Code Playgroud)
并且这样打电话:
char* result_pointer = NULL;
foo(array_of_c_strings, 5, result_pointer);
printf("%s", result_pointer); // here result_pointer is always null
Run Code Online (Sandbox Code Playgroud)
我想初始化函数中的char指针,当调试一切正常时,但是当离开函数的范围时,这个指针再次变为null,即使它离开函数的范围,如何保持初始化?