0
可以初始化字符數組爲什麼的時候,如果數組大小留空,這樣爲什麼這個字符數組賽格故障
char str1[] = "Hello";
該計劃將賽格故障任何人向我解釋,但如果指定了它這樣
char str1[10] = "Hello";
它工作正常。
下面是完整的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char concat_string(char str[], char str2[], char destination[], unsigned int bufferSize);
int main(int argc, char *argv[])
{
unsigned int bufferSize = 64;
// Both str1 and str2 must be defined
// or else the program will seg fault.
char str1[] = "Hello ";
char str2[] = "World";
char concatenatedString[bufferSize];
concat_string(str1,str2,concatenatedString,bufferSize);
printf("The concatenated string is: \n%s\n", concatenatedString);
return 0;
}
char concat_string(char str[], char str2[], char destination[], unsigned int bufferSize)
{
char buffer[bufferSize];
strncat(str, str2, bufferSize);
strncpy(buffer,str, bufferSize);
strncpy(destination,buffer,bufferSize);
return *destination;
}
是的。我是個白癡。這很有道理。 – FrozenWasteland
@FrozenWasteland:我們都是從白癡開始的。製作mistaeks,吸取教訓,沖洗並重復。 –