[英]Why a String variable can not be assigned to another string variable in C?
為什么在下面寫的代碼,我們不能分配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
將一個數組/指針的內容傳輸到一個數組。
當你寫:
char strB [80];
strB不是指針,而是常量指針。 這意味着您不能更改strB指向的地址,因此
strB = strA;
什么也不會做
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.