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 on my currently limited understanding, seems to be acceptable. However, the code below does not seem acceptable:
int * ip; // UPDATED
*ip = 5; // UPDATED
Rather, I must say something like:
int x;
int * ip;
ip = &x;
x = 5;
So with the character string, I can just initialize my pointer to point right at a string literal as soon as I spell it out. I don't have to identify that string literal with any other variable... but maybe this is because of the close relationship between pointers and arrays, and I actually am simultaneously identifying it with the implicit array of same name? (my attempt at partially answering my own question)
But with the integer, I cannot just point to an integer value floating in memory. I must give that integer value a variable name and point at the variable location.
I'm guessing this might have something to do with the differences of stack vs heap storage? Something I'm still a little weak on...
Any further insight would be appreciated! Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…