1
#include <stdio.h>

  int main(void){
   char c[8];
   *c = "hello";
   printf("%s\n",*c);
   return 0;
   }

最近ポインターを勉強しています。上記のコードでエラーが発生します-割り当ては、キャストなしでポインターから整数を作成します[デフォルトで有効になっています]。このエラーに関するSOの投稿をいくつか読みましたが、コードを修正できませんでした。私はcを8文字の任意の配列として宣言しました。cには最初の要素のアドレスがあります。したがって、*c = "hello" を実行すると、1 文字が 1 バイトに格納され、"hello" の他の文字に必要な数のバイトが使用されます。誰かが問題を特定して修正するのを手伝ってください。マーク

4

2 に答える 2

1
#include <stdio.h>

   int main(void){
   char c[8];//creating an array of char
   /*
    *c stores the address of index 0 i.e. c[0].  
     Now, the next statement (*c = "hello";)
     is trying to assign a string to a char.
     actually if you'll read *c as "value at c"(with index 0), 
     it will be more clearer to you.
     to store "hello" to c, simply declare the char c[8] to char *c[8]; 
     i.e. you have  to make array of pointers 
    */
   *c = "hello";
   printf("%s\n",*c);
   return 0;
 }

それが役立つことを願っています.. :)

于 2014-09-01T16:03:04.597 に答える