簡體   English   中英

為什么不能將字符串變量分配給C中的另一個字符串變量?

[英]Why a String variable can not be assigned to another string variable in C?

為什么在下面寫的代碼,我們不能分配strAstrB和指針pA持有指針的地址pB那么應該已經復制上的分配解決pApBstrB應該包含相同的值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將一個數組/指針的內容傳輸到一個數組。

當你寫:

char strB [80];

strB不是指針,而是常量指針。 這意味着您不能更改strB指向的地址,因此

strB = strA;

什么也不會做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM