为什么在下面的代码中我们不能将strA
赋值给strB
,因为指针pA
持有指针pB的地址
那么在将 pA
赋值给 pB
时地址应该被复制,并且 strB
应该包含与 strB 相同的值
。
#include <stdio.h>
char strA[80] = "A string to be used for demonstration purposes";
char strB[80];
int main(void)
{
char *pA; /* a pointer to type character */
char *pB; /* another pointer to type character */
puts(strA); /* show string A */
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
pB = strB; /* point pB at string B */
putchar('\n'); /* move down one line on the screen */
pB=pA;
strB=strA;
puts(strB); /* show strB on screen */
puts(strA);
return 0;
}
最佳答案
您不能在 C 中分配数组 (strB=strA
)。您必须使用 strcpy
或 memcpy
将一个数组/指针的内容传输到一个数组。
关于c - 为什么在 C 中不能将一个字符串变量赋值给另一个字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9412546/