C++ 为一个字符设置值**
C++ Set value for a char**
我正在尝试为 char**
变量赋值。在我的 foo.h
中,我定义了几个变量,例如
#define APIOCTET int
#define APILONG long
#define APICHAR char
#define APISTRING char*
现在在我的 foo.cpp
中,我正在尝试使用一种方法
APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
//open text file to where output will be printed
//do other stuff, etc..
//return result;
}
我想为我的 APISTRING* outputString
赋值,但我不知道该怎么做,我尝试了很多基本上是以下代码的变体
APISTRING error = "error";
APISTRING other = "string";
APISTRING charArr[] = { error, other, error };
APISTRING *charArr2[] = { charArr };
errorString = *charArr2;
我也不是 100% 清楚 APISTRING* outputString
到底是什么。当我尝试编译时,它给我一条错误消息,其中提到它是 char**
。它是二维数组吗?..指向字符数组的指针?..但最重要的是,我将如何为这个变量赋值?提前致谢。
APISTRING* outputString 将在编译时作为 char** outputstring 进行预处理和替换。因此,outputString 将是双指针,因此您需要这样做(代码下方)。为了简单起见,我将 .h 和 cpp 结合在一起。
#include<iostream>
using namespace std;
#define APIOCTET int
#define APILONG long
#define APICHAR char
#define APISTRING char*
APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
APISTRING getIt = *outputString;
cout<<" "<<getIt<<endl;
}
int main()
{
APISTRING str = "hello";
APISTRING* outputString = &str;
APILONG val = apiInitialize("world", outputString );
system("PAUSE");
return 0;
}
我建议使用 std::string,它可以很容易地调整某些行为。希望这有帮助。
APISTRING* 是指向 char 的指针。它持有一个地址,该地址持有内存中字符串的第一个字符的地址。
有关 C/C++ 中双指针的更多信息,请参阅此 question。
要分配给您需要做的字符串 *outputString = "string"
我正在尝试为 char**
变量赋值。在我的 foo.h
中,我定义了几个变量,例如
#define APIOCTET int
#define APILONG long
#define APICHAR char
#define APISTRING char*
现在在我的 foo.cpp
中,我正在尝试使用一种方法
APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
//open text file to where output will be printed
//do other stuff, etc..
//return result;
}
我想为我的 APISTRING* outputString
赋值,但我不知道该怎么做,我尝试了很多基本上是以下代码的变体
APISTRING error = "error";
APISTRING other = "string";
APISTRING charArr[] = { error, other, error };
APISTRING *charArr2[] = { charArr };
errorString = *charArr2;
我也不是 100% 清楚 APISTRING* outputString
到底是什么。当我尝试编译时,它给我一条错误消息,其中提到它是 char**
。它是二维数组吗?..指向字符数组的指针?..但最重要的是,我将如何为这个变量赋值?提前致谢。
APISTRING* outputString 将在编译时作为 char** outputstring 进行预处理和替换。因此,outputString 将是双指针,因此您需要这样做(代码下方)。为了简单起见,我将 .h 和 cpp 结合在一起。
#include<iostream>
using namespace std;
#define APIOCTET int
#define APILONG long
#define APICHAR char
#define APISTRING char*
APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
APISTRING getIt = *outputString;
cout<<" "<<getIt<<endl;
}
int main()
{
APISTRING str = "hello";
APISTRING* outputString = &str;
APILONG val = apiInitialize("world", outputString );
system("PAUSE");
return 0;
}
我建议使用 std::string,它可以很容易地调整某些行为。希望这有帮助。
APISTRING* 是指向 char 的指针。它持有一个地址,该地址持有内存中字符串的第一个字符的地址。
有关 C/C++ 中双指针的更多信息,请参阅此 question。
要分配给您需要做的字符串 *outputString = "string"