In C, why can't an integer value be assigned to an int* the same way a string value can be assigned to a char*?

后端 未结 5 903
北荒
北荒 2020-11-29 09:01

I\'ve been looking through the site but haven\'t found an answer to this one yet.

It is easiest (for me at least) to explain this question with an example.

I

5条回答
  •  迷失自我
    2020-11-29 09:28

    The problem is that you are trying to assign the address 5 to the pointer. Here you are not dereferencing the pointer, you are declaring it as a pointer and initializing it to the value 5 (as an address which surely is not what you intend to do). You could do the following.

    #include 
    
    int main(int argc, char* argv[])
    {
      int *myint, b;
      b = 5;
      myint = &b;
    }
    

提交回复
热议问题