현재 strcpy() 함수의 다른 종류의 동작에 걸린 프로그램을 작성하고 있습니다. 내가 도착 후,strcpy() 제대로 작동하지 않습니다.
내가 strcpy를를 사용 해달라고하면p : y strnng mystr: my strnng
() 함수 - : Strip first and last character from C string
//This program checks whether strcpy() is needed or not after doing the modification in the string
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[] = "Nmy stringP";
char *p = mystr ;
p++;
p[strlen(p)-1] = '\0';
strcpy(mystr,p);
printf("p : %s mystr: %s\n",p,mystr);
}
출력 : 여기에
나는 다음과 같은 질문을했다 ... 그것의 짧은 데모입니다p : my string mystr : Nmy string
왜 이런 일이 발생합니까 ??
같은 스왑을 할 수있는
temp
메모리 위치를 사용할 수 있습니다. – chris'strcpy()'소스와 타겟 버퍼에 메모리를 오버랩 할 수 없습니다. 그렇게하면 동작이 정의되지 않습니다. [문서] (http://en.cppreference.com/w/c/string/byte/strcpy)를 참조하십시오. – WhozCraig
더 깊숙이 들어가기 전에 일반적인 팁 : 결코 strcpy를 사용하지 마십시오. 그것은 너무 많은 나쁜 코드와 너무 많은 시스템 취약점의 원천이었습니다. 'strncpy'가 더 선호됩니다. –