Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your understanding is correct. A string literal is really an array of char, and taking its value really yields a pointer to its first element. You can do this for any literal array (but without the syntax sugar that you have char arrays).

char *s = "String";        // right side is array of char
int *x = (int []){1, 2};  // right side is array of int

Similarly, the following are both incorrect.

char *s = 'S';
int *x = 1;

Note the difference between "String" and 'S'. The former is an array of char, but the latter is just an int.

However (as was first mentioned by Keith), string literals and literal arrays (more generally called compound literals) have different storage duration. In particular, a string literal always has static storage duration; but a compound literal has automatic or static storage duration, depending if it appears in a function or not. This means that if you take a pointer to a compound literal from within a function:

  • You can read and change change it until execution goes past the end of its block, and
  • You must not access it in any way after execution goes past the end of its block. Contrary, with string literals, you can read them after that, because they have static storage.

In this respect, taking a pointer to a literal array is very similar to just declaring an array (except, for example, sizeof()):

int x[] = {1, 2};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...