如果您打算在运行时改变它,那么你可以使用任何下列选项:
char result[] = "invalid"; // 8 bytes in the stack
static char result[] = "invalid"; // 8 bytes in the data-section
如果您不打算在运行时更改它,则可以使用以下任一选项:
const char result[] = "invalid"; // 8 bytes in the stack
static const char result[] = "invalid"; // 8 bytes in the data-section
const char* result = "invalid"; // 8 bytes in the code-section, and a pointer (4 or 8 bytes) in the stack
static const char* result = "invalid"; // 8 bytes in the code-section, and a pointer (4 or 8 bytes) in the data-section
如果你想在运行时只在稍后将其初始化:
char result[] = "invalid"; // 8 bytes in the stack
static char result[] = "invalid"; // 8 bytes in the data-section
...
strcpy(result,"MMM");
// But make sure that the second argument is not larger than the first argument:
// In the case above, the size of "MMM" is 4 bytes and the size of 'result' is 8 bytes
由于这是C++:'的std :: string结果( “无效”);' – Borgleader
分配?不能使用'std :: string'? –
'strncpy()'为你的情况呢?再次:使用'std :: string'使[tag:C++]更简单! –