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 900
北荒
北荒 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:22

    When you do char* mystr = "foo";, the compiler will create the string "foo" in a special read-only portion of your executable, and effectively rewrite the statement as char* mystr = address_of_that_string;

    The same is not implemented for any other type, including integers. int* myint = 5; will set myint to point to address 5.

提交回复
热议问题