Search code examples
cpointersdereference

How to understand the pointer star * in C?


I'm struggling with the pointer sign *, I find it very confusing in how it's used in both declarations and expressions.

For example:

int *i; // i is a pointer to an int

But what is the logic behind the syntax? What does the * just before the i mean? Let's take the following example. Please correct me where I'm wrong:

char **s;
char *(*s); // added parentheses to highlight precedence

And this is where I lose track. The *s between the parantheses means: s is a pointer? But a pointer to what? And what does the * outside the parentheses mean: a pointer to what s is pointing?

So the meaning of this is: The pointer pointing to what s is pointing is a pointer to a char?

I'm at a loss. Is the * sign interpreted differently in declarations and expressions? If so, how is it interpreted differently? Where am I going wrong?


Solution

  • The rule of declaration in c is, you declare it the way you use it.

    char *p means you need *p to get the char,

    char **p means you need **p to get the char.