Why must int pointer be tied to variable but not char pointer?

前端 未结 8 687

I am not sure if I worded the question correctly, but here it is spelled out:

char * cp = \"this is a char pointer\";

The code above, based

8条回答
  •  不思量自难忘°
    2020-12-10 10:33

    The simplified reason is that a string is a value that does "float in memory". The compiler actually takes the string and puts it in your executable (or library), and when you load the program into memory, the string has an address, and when you use the string you're actually getting a pointer to that address.

    But for integers, the compiler doesn't automatically put the value in memory. It might do, if you tell it to explicitly (using an assignment into a variable and then taking the address of the variable). But in some cases it may choose to store it in a register, which doesn't have an address. It may also choose not to store it at all, depending on optimisation options.

提交回复
热议问题