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 904
北荒
北荒 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:20

    "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*?"

    Because it's not even a similar situation, let alone "the same way".

    A string literal is an array of chars which – being an array – can be implicitly converted to a pointer to its first element. Said pointer is a char *.

    But an int is not either a pointer in itself, nor an array, nor anything else implicitly convertible to a pointer. These two scenarios just don't have anything in common.

提交回复
热议问题