-7

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?

0

4 Answers 4

6
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:

p2 is 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
  • but p2 is the name of the array and it is also apointer to this array of pointers to ints! so int *p2[5] is also a pointer to pointers to ints!!! Commented Jan 22, 2014 at 13:51
  • 2
    It sounds to me like you've already decided that you know the answer. Why are you even asking the question? Commented Jan 22, 2014 at 13:51
  • 3
    I told you that 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. Commented Jan 22, 2014 at 13:55
  • 1
    First of all you need to slow down. You've got a lot of learning to do and it will take time. Don't expect instant experthood. Go back to your text book and read about arrays and pointers again. And read the links I have given you in comments. Commented Jan 22, 2014 at 14:57
  • 1
    An array is not a pointer as is explained in the various links I have given you. Since you have made it clear that you won't read anything we offer you, why would anyone here spend the time writing yet another explanation that seems certain to be ignored? And you don't need to cast. If you have int a[5], you can write int pa = a because an array *decays to a pointer. Commented Jan 23, 2014 at 7:11
2

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
  • see my comment to david hofferman post Commented Jan 22, 2014 at 13:53
  • @user3223696 So what is the question that is not answered here? You wanted to know the difference, and this highlights the difference. It also shows that you can use them for the same thing at times, but they are not inherently the same. What questions do you still have? Commented Jan 22, 2014 at 13:56
  • ut as in regular array int arr[5] arr is a constant pointer to &arr[0] and why in this case int *p2[5] p2 is not a pointer to the first pointer in the array of 5 pointers to int????? Commented Jan 22, 2014 at 14:00
  • 2
    @user3223696 Sorry - I am done talking. Look through the answers and see if you can figure it out for yourself. Just because you ask a question and throw lots of capital letters and repeated question marks doesn't mean you can demand answers from everyone. Everyone here has lots of better things to do than take abuse. Perhaps if you asked the questions fully and completely, or read the FAQ on how to ask a question better. Show the code you are trying and the problems you are having and you will have better luck. Commented Jan 22, 2014 at 14:43
  • 1
    You don't need to cast Commented Jan 23, 2014 at 7:12
2

What is the different between int **p and int *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
  • i think i get it but why u say in last line that p is variable of type array of pointer to int? p is array of pointers? so the array name is pointer to aconstand memory block of the first element ? so why u cant say that p is apointer itself! to the first elements?? why u say its array of pointer to int? Commented Jan 23, 2014 at 6:53
  • hmmm... so why am I getting test.c:8:7: warning: incompatible integer to pointer conversion initializing 'int **' with an expression of type 'int' [-Wint-conversion] int **p = 123; Commented Sep 28, 2019 at 17:49
  • @PedroA: 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! Commented Sep 28, 2019 at 20:19
0

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.

3
  • check my edited post this pointed pointer will point to array of ints later , Commented Jan 22, 2014 at 13:56
  • In the second case first dimension is fixed => 5, in first case, it just something that can point two dimensional array, but not necessary it will point two dimensional array Commented Jan 22, 2014 at 14:00
  • check my comments on david post answer me there please Commented Jan 22, 2014 at 14:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.