c - 为什么在 C 中不能将一个字符串变量赋值给另一个字符串变量?

标签 c string pointers

为什么在下面的代码中我们不能将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)。您必须使用 strcpymemcpy 将一个数组/指针的内容传输到一个数组。

关于c - 为什么在 C 中不能将一个字符串变量赋值给另一个字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9412546/

相关文章:

c - 如何让我的代码显示 "You can' t 除以 0"

c - 通用函数 输入数组

c - 错误 C2059 定义 max() 函数时

C 编程帮助 Int2String 方法

c++ - 字符串连接错误

C 为什么将数组的首地址传递给 char 指针会提供整个字符串?

c - *p = array[i] 的效果?

c - 如何在 C 中为两个 for 循环编写 SSE 指令?

javascript - 奇怪的 JavaScript 换行符拆分行为

c++ - 使用指针复制构造函数