# pointer doubts...

This is a discussion on pointer doubts... within the C Programming forums, part of the General Programming Boards category; Hi, I have following code Code: main() { int a[3][4] = { 1,2,3,4, 5,6,7,8, 9,10,11,12 }; printf("\n%u\n%u\n%u\n%u\n%u\n",a[0]+1,*(a[0]+1),*(*(a+0)+1),**(a+1),(&a+1)); ; return 0; ...

1. ## pointer doubts...

Hi,

I have following code
Code:
```main()
{
int a[3][4] = { 1,2,3,4,
5,6,7,8,
9,10,11,12
};
printf("\n%u\n%u\n%u\n%u\n%u\n",a[0]+1,*(a[0]+1),*(*(a+0)+1),**(a+1),(&a+1));
;

return 0;
}```
1. Why does **a take me to the first element in the second row? Shouldn't it point to the address of content of a[0] . As per the double pointer..

2. why does (a+1) takes me to the 2nd row and (&a+1) takes me to the end of array? Isn't a and &a one and the same?

2. Originally Posted by sanddune008
1. Why does **a take me to the first element in the second row? Shouldn't it point to the address of content of a[0] . As per the double pointer..
**a is equivalent to a[0][0], so it should give you the value of the first element of the first array.

Originally Posted by sanddune008
2. why does (a+1) takes me to the 2nd row and (&a+1) takes me to the end of array? Isn't a and &a one and the same?
a is a two dimensional array. It is converted to a pointer to the first array. Hence, (a+1) is a pointer to the second array. But &a is the address of the array itself. Hence, &a+1 is the address of the next two dimensional array (even though such an array does not exist).

A few things to note:
• Include the appropriate headers. In this case #include <stdio.h>
• main should return an int, so declare it as such instead of relying on default int.
• It is good practice to make full use of braces when initialising a two dimensional array (except possibly when performing zero initialisation, which is not what you are doing here).
• Pointers should be printed with the %p format specifier. You should cast the corresponding arguments to void*.
• Your indentation could be improved.

3. 1 - You don't actually have **a anywhere in your code. You have **(a+1).
2 - a is not a pointer. You can't act like it is all the time. It doesn't work exactly the same. Start here: Arrays and Pointers

Quzah.

4. Originally Posted by laserlight
**a is equivalent to a[0][0], so it should give you the value of the first element of the first array.
[/list]
but the same double pointer behaves differently in code below
Code:
```int **p,*a =8;
p=&a;```
My question is why does the double indirection behaves differently with an array declaration? How does the compiler deduce this?

5. why?

6. You are abusing pointers terribly, and should really start compiling with warnings on. Why are you assigning a pointer the value of 8? You really should read the link in my previous post, and all associated links on that page.

Quzah.

Originally Posted by quzah
You are abusing pointers terribly, and should really start compiling with warnings on. Why are you assigning a pointer the value of 8? You really should read the link in my previous post, and all associated links on that page.
Quzah.
I am not abusing....I am trying to understand the behaviour of double indirection(**).

Code:
```p= &a;
*p; // This will give me address of 8
**p; // This will give me value 8```
But why is not the same with **a (array pls see the first post)

besides this let go through the link u mentioned earlier..

8. You can't assign a constant to a pointer. It's decide by the compiler which address to assign the variable and the pointer can point to the variable's address. So, in effect this is wrong
int *a=8;

9. >> But why is not the same with **a (array pls see the first post)

Because a multidimensional array of int's is not the same as an array of pointers to ints. In other words, 'a' is just the starting address of 3 * 4 * sizeof( int ) contigious blocks of data.

Let's say a[ 0 ][ 0 ] is set to 1024. Interpreted as an int*, *a is this first block (1024). Fine. But interpreting it as an int**, **a would load the contents of *a (1024) and reinterpret it as a pointer! Obviously not what you want.

10. Perhaps I also got doubtful. Is assigning a constant to a pointer legal in C? What actually happens when we do that ?

11. Giving a pointer a hard coded address is like forwarding your mail before you move out. You're saying you know exactly where something should be stored. It's legal, just boneheaded all the way around.

12. >> Is assigning a constant to a pointer legal in C?

It's legal, but usually not a very good idea.

>> What actually happens when we do that ?

It just sets it to that particular address. Back in the old days, that was sometimes useful, as you could, say, access video memory directly via a known address (which didn't always work, of course, depending on the actual configuration and capabilities of the computer). Nowdays, this sort of thing is quite rare, and in fact the only hard-coded address you generally see is 0!