#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *p ;
char *q ;
char *l = "aaaaaasdfsfdqwecvxcxfwdsagfd" ;
p = (char*)malloc(sizeof(l)) ;
q = (char*)malloc(strlen(l)) ;
strcpy(p, l) ;
strcpy(q, l) ;
printf("%s\n",p) ;
printf("%s\n",q) ;
printf("%d\n",strlen(l)) ;
return 0 ;
}
请看看这个,在23个字符内输入一些字符的时候,没问题,输入的字符超过23个,就像图片显示的那样,我的结果显示乱码,为什么会这样? enter image description herec sizeof()会导致溢出?
不要将代码作为图像发布,这太可怕了。而你没有分配足够的内存,缺少null-termination字符。可能是这个问题,但现在我必须进行眼科手术,所以你会原谅我。 –
我很抱歉,这是我第一次问stackoverflow问题,我会解决它。谢谢。我不认为插件空终止字符在这里工作,它已经存在了 –