-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?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067

4 Answers4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 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!!! – user3223696 Jan 22 '14 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? – David Heffernan Jan 22 '14 at 13:51
  • i dont know the answer so im asking u – user3223696 Jan 22 '14 at 13:53
  • 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. – David Heffernan Jan 22 '14 at 13:55
  • @DavidHeffernan Great point. The array can be cast as a pointer and used as a pointer. Also, a pointer can be iterated over like an array if it points to an array instead of an individual element. However, a pointer and an array are not the same. – Trenin Jan 22 '14 at 13:57
  • but 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????? – user3223696 Jan 22 '14 at 13:58
  • No. `arr` is an array. It is not a pointer. I've told you that repeatedly now. – David Heffernan Jan 22 '14 at 13:59
  • so how u explain that int *p = arr will make p point to the same array that arr points to! HELP! – user3223696 Jan 22 '14 at 14:01
  • Formally, `p` is a pointer, and `arr` is an array. An array is not a pointer. – David Heffernan Jan 22 '14 at 14:01
  • but array names can converted to pointers! how u settle this mess? – user3223696 Jan 22 '14 at 14:03
  • @user3223696 read how sizeof treats arrays and pointers, probably it will help to understand the difference – Dabo Jan 22 '14 at 14:08
  • You should have a good read of your text book. An array decays to a pointer in many contexts. – David Heffernan Jan 22 '14 at 14:09
  • explain pls why u can treat array name as pointer what is the array name is it pointer?? int arr[5]; int *p=arr; now p points to the arr[0] why arr is not pointer we assigned the pointed memory address by him to p!!! help!!! IM CONFUSED!!!! – user3223696 Jan 22 '14 at 14:12
  • http://stackoverflow.com/questions/1461432/what-is-array-decaying Please stop shouting – David Heffernan Jan 22 '14 at 14:15
  • read everything not helping me i not understanding ! help – user3223696 Jan 22 '14 at 14:18
  • @user3223696 You may as well consider them the same thing. For your purposes, they "point" to the same types of data. 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. However `int**p` reserves no such room, and is simply a pointer to a pointer to an `int`. It might be pointing to an array, or it might be an array of pointers. It might even be a 2-D array of ints. – Trenin Jan 22 '14 at 14:21
  • Reading is the only form of communication we have. If you won't read your textbook then there's very little we can do for you. Demanding knowledge doesn't work. – David Heffernan Jan 22 '14 at 14:25
  • @trenin its not my question! dont worry int **p . p will point to pointer that will use malloc to get 5 pointers that pointing to int! my question is why array name is not pointer??? int arr[5]; WHY why &(arrx[0]), arr not equal?? ?? WHY NOT EQUALL????? WHY! – user3223696 Jan 22 '14 at 14:26
  • @user3223696 They are equal. Show me the code where they aren't. – Trenin Jan 22 '14 at 14:27
  • Fundamentally it seems that your problem is that you do not understand the difference between an array and a pointer. – David Heffernan Jan 22 '14 at 14:30
  • explain me please i think they are the same but with different size and array can contain more elements array name is pointer to constant location while pointer is not! im wrong???????????????????? – user3223696 Jan 22 '14 at 14:34
  • Read this: http://c-faq.com/aryptr/index.html Also, which textbook do you use? – David Heffernan Jan 22 '14 at 14:40
  • WHAT TO READ??? what i need to read??? i learn from presentations good? – user3223696 Jan 22 '14 at 14:41
  • 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. – David Heffernan Jan 22 '14 at 14:57
  • why david hofferman say array name is not pointer??? why i cant simply use it as pointer why i need to cast it to int * like (int*) – user3223696 Jan 23 '14 at 06:41
  • 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*. – David Heffernan Jan 23 '14 at 07: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.

Trenin
  • 2,041
  • 1
  • 14
  • 20
  • see my comment to david hofferman post – user3223696 Jan 22 '14 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? – Trenin Jan 22 '14 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????? – user3223696 Jan 22 '14 at 14:00
  • @user3223696 It is pointing to the first pointer in the array of 5. – Trenin Jan 22 '14 at 14:17
  • why &(arrx[0]), arr not equal?? – user3223696 Jan 22 '14 at 14:23
  • @user3223696 When you cast it to a pointer, they will be equal. What makes you think they aren't equal? – Trenin Jan 22 '14 at 14:25
  • WHY U NEED TO CAST IT TO POINTER its already pointer to the first element! int *p; int arr[5]; p=arr // now p will point to arr[0] so arr is a pointer!! i dont need to cast it to pointer!! why they are not equal??? – user3223696 Jan 22 '14 at 14:27
  • @user3223696 Am I talking to a wall??? They ARE equal!!!!! – Trenin Jan 22 '14 at 14:29
  • so why u said u need to cast the array name to int * so they will be equal?????????????????????????????? – user3223696 Jan 22 '14 at 14:29
  • @user3223696 Why do you demand me to justify my statement "when you cast to a pointer" when you won't justify why you think they aren't equal? Show me the code you are testing where you get different values. – Trenin Jan 22 '14 at 14:31
  • Why are you casting. The array will decay to a pointer. – David Heffernan Jan 22 '14 at 14:32
  • i think they are equal .. but if yes why u need to cast arr name to int *???? – user3223696 Jan 22 '14 at 14:33
  • Who says you do? `if (arr == &arr[0]) printf("Equal\n");` prints "Equal". I suggested that you cast it to a pointer to get the same datatype and be able to convert one type to other. But when you do comparisons like this, you are dealing directly with the addresses anyway. – Trenin Jan 22 '14 at 14:35
  • u not understand me pointer and array name is the same?? – user3223696 Jan 22 '14 at 14:38
  • 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. – Trenin Jan 22 '14 at 14:43
  • sorry please help me.. i dont understand ! pointer is the same as array name? ?? – user3223696 Jan 22 '14 at 14:44
  • @user3223696 When you ask a question and get an answer, do you really think asking the same question will get you a different answer? Take a look at where you asked this exact question, and then collect all the answers. Then ask any new questions, or point out WHY you think your question is still not answered. Simply asking the same question again will get you nowhere... – Trenin Jan 22 '14 at 15:03
  • why david hofferman say array name is not pointer??? why i cant simply use it as pointer why i need to cast it to int * like (int*) – user3223696 Jan 23 '14 at 06:41
  • 1
    You don't need to cast – David Heffernan Jan 23 '14 at 07:12
  • some u say its already apointer itself?? help! – user3223696 Jan 24 '14 at 09:11
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?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 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? – user3223696 Jan 23 '14 at 06: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;` – PedroA Sep 28 '19 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`! – Eric Lippert Sep 28 '19 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.

Dabo
  • 2,371
  • 2
  • 18
  • 26