谁能解决问题,重谢!
在GCC下编译,运行错误。在VC下,没问题代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int count = 5;
char *p, *q;
int i, j;
char **trans = (char**)malloc(count * 4);
char buf[] = "hahahahahha";
//q = (char*)malloc(strlen(p) + 1);
//strcpy(q, "hiiii");
//printf("%p: %s\n", q, q);
trans[0] = (char*)malloc(strlen(buf) + 1);
strcpy(trans[0], "hello");
for (i = 1; i < count; i++)
{
trans[i] = (char*)malloc(strlen("qwerty") + 1);
strcpy(trans[i], "qwerty");
printf("%p: %s\ttrans[0]: %s\n", trans[i], trans[i], trans[0]);
}
//printf("%p: %s\n", q, q);
//free(q);
getchar();
return 0;
}
GCC下运行的结果:
0x1fd7050: qwertytrans[0]: hello
0x1fd7070: qwertytrans[0]: hello
0x1fd7090: qwertytrans[0]: hello
0x1fd70b0: qwertytrans[0]: ?p?
第四次输出,trans[0]所指的内容发生了变化,但我不知道为什么