0
누군가가 왜 내게 설명 할 수 있습니다 배열 배열 크기가 비어있는 경우,이왜이 char 배열 Seg 오류가 발생합니까?
char str1[] = "Hello";
프로그램이 seg 오류가 있지만이 지정되어있는 경우
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 : 우리 모두 바보로 시작합니다. mistaek을 만들고, 수업을 배우고, 헹구고, 반복하십시오. –