Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
*a is pointer a, so what is **a ?
Help me !
Posted
Comments
Maximilien 25-Aug-11 11:24am    
it's a pointer to a pointer. (too short for a real answer).
Simon Bang Terkildsen 25-Aug-11 11:28am    
Short but true
Albert Holguin 25-Aug-11 16:30pm    
don't think answers need to be a certain length... you can always follow up with a link for reference...

Actually, *a is only syntactically correct if a itself is a pointer, and in that case *a can not be a simply because the types don't match, no matter what type a is defined to point to.

What you probably mean is the syntax used to define a (pointer) variable:
C++
int f1() {
   int a; // this is a variable of type int
   a = 1;
   return a; // this will return the value 1
}
void f2() {
   int *a; // this is a pointer to a value of type int
   int b;
   a = &b; // now a points to the contents of b
   b = 2;
   return *a; // returns the value of b, which is 2
}
void f3() {
   int **a; // this is a pointer to a pointer to a value of type int
   int *b;
   int c = 3;
   b = &c; // ok, b points to the contents of c
   a = &b; // a now points to the contents of b which points to the contents of c
   return **a; // returns 3
}
 
Share this answer
 
Comments
CPallini 26-Aug-11 5:04am    
A code sample is worth thousand words. My 5.
Ashish Tyagi 40 6-Sep-11 0:11am    
Best answer for a novice. my 5
In the statement:
C++
int *p; 

p is a pointer (not *p)
The variable declaration may be interpreted as follows:
*p is an int, hence p is a pointer to an int.

A similar reasoning gives
C++
int ** q;

q is a pointer to a pointer to an int.

Generally speaking, the expressions *a, **a cannot be interpreted correctly without context.
For instance, the statement
C++
printf("%d\n"), **q);

(if correct) tell us q is a double pointer.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Aug-11 19:48pm    
Good stuff, a 5.
--SA
CPallini 26-Aug-11 2:48am    
Thank you.
Stefan_Lang 26-Aug-11 4:58am    
My 5 for being the first to not fall into the trap of mixing up dereferences with pointer definitions :-)

Or at least you beat me to the posting...
**a is pointer of *a.
Otherway its a representation of 2 dimention pointer.
concider this:
C++
a[] is array of a.
 AND
a[][] is array of a[].


suppose you have:
#define m=3
#define n=2
/////////////
...
int a[m][n];
int i,j;
for(i=0;i<m;i++)>
for(j=0;j<n;j++)>
{
///////
}

Like  this you can access:
int **a;
//use alloc or malloc to initialize the 2 dimentional pointer

for(i=0;i<m;i++)>
for(j=0;j<n;j++)>
{
///////
}
 
Share this answer
 
It is called as double pointer,most of the times used to point to the dynamically created two dimensional arrays.
The following link has few more discussions:
http://www.daniweb.com/software-development/c/threads/69966[^]
 
Share this answer
 
v3
Comments
Smithers-Jones 25-Aug-11 13:50pm    
I corrected your link.
hakz.code 26-Aug-11 0:14am    
Thanks,I was wondering why the hyperlink was not working!
Stefan_Lang 26-Aug-11 4:56am    
Actually it's a double dereference, not a double pointer. ;-)

Of course, everyone knows what we're talking about, but we should be more concise when explaining basic C syntax to a beginner. See C.Pallini's response (and mine - but C.Pallini beat me to it)
Does this[^] help?
 
Share this answer
 
Comments
Albert Holguin 25-Aug-11 16:32pm    
Good link, +5
Sergey Alexandrovich Kryukov 25-Aug-11 19:47pm    
It is good, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900