#include "stdio.h"
#include "string.h"
main()
{
char string[] = "october"; // october is 7 letters
strcpy(string, "september"); // september is 9 letters
printf("the size of %s is %d and the length is %d\n\n", string, sizeof(string), strlen(string));
return 0;
}
출력 :SIZEOF STRLEN 대
9 월 크기가 8이고 길이가 9
내 구문 또는 무엇에 문제가 무엇입니까?
'string' 배열의 끝에서 쓰고 있습니다. 이것은 정의되지 않은 동작입니다. 'string'은 8 문자 ("10 월"은 7, 널 종결자는 1) 만 저장할 수 있습니다. 'strcpy'를 호출하면, 10 문자를 쓰고 있습니다 (9 개는 "9", 널 종결자는 1 개). 이것은 배열의 끝을 지나치고 인접한 메모리를 덮어 쓰는 것을 의미합니다. – Marlon
'sizeof'는'strlen'가 실행 시간 인 * compile * time에서 계산됩니다. – Naveen
@Naveen : VLA가 관련되어있는 경우 반드시 그런 것은 아니라는 점에 유의하십시오. – caf