What is the different between int **p and int *p2[5] in the first case p is a pointer to pointers to an array that i will define later!,
but p2 also point to array of pointers!!!!.
int *p2[5] is the same as int p2[5];.
Because in both cases p2 is pointer !.
what is the difference?
4 Answers
int **p
This is a pointer to pointer to int. So p could point to a single pointer, or an array of pointers. In turn *p could point to a single int, or an array of int. Exactly what is behind p is not expressed in its type.
int *p2[5]
This is an array, of length 5, of pointer to int. Here, p2 is an array of length 5. No doubt about that. But what about p2[i]? They could be pointers to a single int, or pointers to an array. Again, the information is not present in the type.
In the question you state:
p2is a pointer.
That statement is not correct. p2 is an array, and not a pointer. In certain contexts, p2 may decay to a pointer, but it is not in itself a pointer.
Note that if you start with the variable p2, then you can assign it to p like this:
p = p2;
This is an array, p2, decaying to a pointer. At this point you know that p points to an array of pointer to int.
However, if all you have is some p of type int** then you do not know the p points to an array. It might not. In other words, the type of a pointer does not fully describe what it points to.
You seem to be getting quite confused about the difference between an array and a pointer. They are not the same. An array is not a pointer. An array can decay to a pointer. A pointer can point at an array.
Perhaps the key difference is that an array always refers to the same variables. On the other hand, a pointer can be made to point at different variables. Another obvious difference is that an array variable defines storage. On the other hand, a defining a pointer variable does not. You must always assign the pointer to the address of some other object.
26 Comments
p2 is an array. But you are insisting that it is a pointer. It is not. You asked what p and p2 are, and I told you. So, p2 is an array and not a pointer. True, p2 can decay to a pointer in certain contexts. But p2 is not a pointer.OK - I see what you are asking.
When you have int arr[5] you can cast it as an int* and it will point to the first element in the array. For example;
int arr[5];
printf("%p == %p\n", &(arr[0]), (int*)arr);
Will print two equal values.
Similarly,
int *p2[5];
int **p=(int**)p2;
printf("%p == %p\n", &(p2[0]), p);
Will also print 2 equal values. So, there is no different in what p2 and p point to.
However, when you declare int* p2[5] you are allocating room for 5 pointers to int and p2 will point to the start of this array when you cast it to a pointer.
On the other hand, a declaration of int**p reserves no such room. It is simply a pointer to a pointer to an int. It might be pointing to an array of ints, or it might be an array of pointers to int. It might even be a 2-D array of ints.
20 Comments
What is the different between
int **pandint *p2[5]?
The way to think about this is: int **p means that the expression **p is a variable of type int. That is, you could say **p = 123. Similarly, int *p2[5]; means that the expression *p2[i] is a variable of type int; you can say *p2[i] = 123;.
Now that you know that, you can deduce the meaning of p. If **p is a variable of type int then *p must be a variable of type pointer to int, and therefore p must be a variable of type pointer to pointer to int.
What about *p2[i]? Now we must know the precedence. Subscript is higher precedence than indirection, so this is *(p2[i]) and it is a variable of type int. Therefore p2[i] must be a variable of type pointer to int, and therefore p2 must be a variable of type array of pointer to int.
Make sense?
3 Comments
test.c:8:7: warning: incompatible integer to pointer conversion initializing 'int **' with an expression of type 'int' [-Wint-conversion] int **p = 123;int **p = x; is a short way of writing int** p; p = x; It is not a short way of writing int **p; **p = x; because that doesn't make sense. The assignment in the declaration assigns to the declared variable, not to the variable that is **p!in the first case p is a pointer pointers to an array
No int **p is pointer to pointer, which can become pointer to block of memory, containing pointers to blocks of memory containing integers.
int *p2[5] , yes, this is array of five pointers.