【发布时间】:2013-03-11 09:11:07
【问题描述】:
我的字符指针指向一些内存说“Hello world”,我想将它与其他指针进行比较,然后想执行 strcpy。我坐在可以做 char *
char *A ="hello"
char *B ="";
strcmp(A,B); // this compares correctly because B points to diff string
strcpy(B,A); // will this statment copies the string alone or both will point to same memory
【问题讨论】:
-
嗯,你可以很容易地检查这个。
-
指针“B”指的是只读内存位置,因此您将无法复制字符串。检查这个问题stackoverflow.com/questions/4051347/…
-
char *B是只读的,您必须char B[6]或char *b = malloc(strlen(a) + 1)才能获得复制空间 -
你必须先通过 malloc(6) 为 B 分配内存;获取字符串的新副本
-
那么问题是什么?
标签: c visual-c++