만들기 최소한의 변경 :
#include <string.h>
#include <stdio.h>
int main()
{
char current[2] = { 0x0, 0x0 }; // Will be null terminated
char chs[100] = "Hello World";
char token[100] ;
int pos = -1; // Destination of strcat must also be null terminated
token[0] = '\0' ;
// String literals does not actually have " in memory they end in \0
while((current[0] = chs[++pos]) != '\0')
{
strcat(token, ¤t[0]); // Take the address of the first char in current
}
printf("%s\n", token) ;
return 0 ;
}
strcat
소스 및 대상이 null로 끝나는 문자열이 될 것으로 기대하고있다. 귀하의 경우에는 current
이 방금 \001
으로 끝나고 그 다음에 메모리에 널 종료자가 오는 것 같습니다.
@mbratch 결과가 Hello World가되어야합니다. – Foredoomed
@Foredoomed 문자열 리터럴을 둘러싼 따옴표를 제거하려고합니까? 따옴표는 실제로 문자열의 일부가 아닙니다! 아마 [책] (http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)을 읽어야합니다. – Praetorian
@Foredoomed,하지만 이것은 단지'chs'에서'token'까지의 문자열 복사라고 생각합니까? 그렇다면'strcpy'가 그 것이다. 당신은'strcpy (token, chs);'를 실행하고 끝낼 것이다. 루프가 필요하지 않습니다. 또는 다른 목적이 있습니까? 'strcat'을 사용하고 싶다면, 두 문자열 매개 변수가 모두 0으로 끝나야합니다. 그래서 적어도 토큰 [0] = '\ 0'을 루프 앞에서 시작하도록 설정해야 할 것입니다. – lurker