0

I am trying this:

int test(int **a){

  return **a;

}

int main(){

  int p[2]={1,2};

  int *a=&p[0];

  int **d=&a[0];

  //for (int i=0;i<2;i++)
     // printf("\na=%d\n",a[i]);   //ok this works


  for (int i=0;i<2;i++)
  printf("\n%d \n",test(&a[i]));




return 0;
}

I want this printf("\n%d \n",test(&a[i])); to work.
I want to use a pointer.

Thanks

3
Contributors
9
Replies
71
Views
2 Years
Discussion Span
Last Post by Banfa
0

I want this printf("\n%d \n",test(&a[i])); to work.

What do you think line 13 is doing? Or what do you hope to achieve with it?

Then check what you passing to the test function.

Edited by 2teez

0

I want to be able to pass in test function a pointer with "&".

With line 13 ,the d pointer points to a pointer which points to p.

0

Hi glao,

With line 13 ,the d pointer points to a pointer which points to p.

That is what you intended doing. But that is not what you are doing with int **d = &a[0]
So, instead of int **d = &a[0] like you did, what you wanted is int **d = &a. Look at it for a while and see why that is different.

I want to be able to pass in test function a pointer with "&".

Then you could do like so:
printf("\n%d \n", test(&a)); not printf("\n%d \n",test(&a[i]));

Turn up -Wall when compling your programs and you will pick some of these warnings.

Moreso, in Line 20 of the original post, why are you not using the variable d?

0

This int **d = &a isn't the same with thisint **d = &a[0] ?
Because the last points to the first element of a and this is the way we initialize pointers,right?

Also, doing printf("\n%d \n", test(&a)); ,prints only "1" , the first element of p.

Can you explain me? :)

Thanks

0

..prints only "1" , the first element of p.

Yes! What about if you do like so:

  for (int i = 0; i < 2; i++, a++)
       printf("\n%d \n",test(&a));

OR

  for (int i = 0; i < 2; i++){
      printf("\n%d \n",test(&a));
      a++;
  } 

You got the point?!!!

Since, we know that a pointer is that that has an address as a value, so a pointer of pointer, should have the address of a pointer as a value. That shows the difference.

Hopes that helps

Edited by 2teez

0

Ok!I will study that.

But ,I can't understand why int **d=&a[0]; is wrong and int **d=&a; is right?

(although I still get the right result but it shows

warning: initialization from incompatible pointer type
)

0

warning: initialization from incompatible pointer type

That is when you use int **d = &a[0];

Ok!I will study that.

May be the following could help:
Try comparing the addresses like so, and see:

  printf("&p[0] has %p, %d\n", &p[0], p[0]);  
  printf("&p[1] has %p, %d\n", &p[1], p[1]); 
  printf("&a    has %p, %d\n", &a, *a);  
  printf("&a[0] has %p, %d\n", &a[0], a[0]);  
  printf("&a[1] has %p, %d\n",  &a[1], a[1]);  
  int **d = &a;  
  printf("**d   has %p, %d\n", d, **d); 

Edited by 2teez

0

Ok, thanks I understand this ,I can't understand why int **d=&a[0]; is wrong.

Anyway ,thanks for the help.

0

In int **d=&a[0]; d has type int** and a has type int*.

The question is if a has type int* what is the type of the expression &a[0]? This isn't to hard to determine because using the array index operator gets rid of a level of indirection so if a has type int* then a[0] has type int. Then, since & adds a level of indirection (i.e. takes the address of), if a[0] has type int then &a[0] has type int*.

So you are assigning an int* to an int** and that is wrong assignments should always be between variables of the same type (or at least between variables with a well defined implicit conversion).

Another way to think of this is that a[0] can be directly replaced by *a they both return the thing a points to. Subsituting that into &a[0] gives &*a the &* cancel, you dereference then take the address of, ending up where you start so &*a is the same as a and therefore int **d=&a[0]; is the same as int **d=a; which is not what I think you want.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.