Kinda of a noob so don't kill me here
Working out what a type means in C can be tricky even for experts. No worries.
What's the difference between the following codes?
The other answers are good and I have no intention of contradicting them. Rather, here's yet another way to think about it. We need to define three things:
A variable is a thing that supports three operations. The fetch operation takes a variable and produces its current value. The fetch operation has no notation; you simply use the variable. The store operation takes a variable and a value, and stores the value in the variable. The address operation takes a variable and produces a pointer.
A pointer is a thing that supports one operation. The dereference operation, written as prefix *pointer
, takes a pointer and produces a variable. (Pointers support other operations such as arithmetic and indexing -- which is a form of arithmetic -- but let's not go there.)
An array is a thing that supports one operation. The index operation takes an array and an integer and produces a variable. It's syntax is postfix: array[index]
OK, so now we come to your question. What does the declaration
int p;
mean? That the expression p
is a variable of type int
. Note that this is a variable; you can store things to p
. What does the declaration
int *p;
mean? That the expression *p
is a variable of type int
. Now that we know that we can deduce what p
is. Since *p
is a dereference and produces a variable, p
must be a pointer to int. What does the declaration
int *p[100];
mean? It means that *the expression *p[i]
is a variable of type int
provided that i
is an integer value from 0
to 99
. We got a variable out, but we could have gotten there from either the pointer or the array, so we have to figure out which. We consult the operator precedence table and discover that the indexing operator binds "tighter" than the dereferencing operator. That is:
*p[i]
is the same thing as
*(p[i])
and remember, that thing is a variable of type int
. The contents of the parens are dereferenced to produce a variable of type int
so the contents of the parens must be a pointer to int
. Therefore
p[i]
is a pointer to int. How is that possible? This must be a fetch of a variable of type pointer-to-int! So p[i]
is a variable of type pointer to int. Since this is an index operation, p
must be an array of pointers to int
.
Now you do the next one.
int (*p)[100];
means what?