2

This is what I'm trying to achieve. Inside pointer 'r' I have the address of pointer 'p', now I try to move the value of 'p'(which is an address) using pointer arithmetic by calling function moveforward(). I get this error :

void value not ignored as it ought to be
movingresult=moveforward(r);

What's wrong here? It's a bit complicated though dealing with pointer to pointer.

#include <stdio.h>

void moveforward(int** y) {
     *y = *y+1;
     printf("value of *y in function : %p\n",*y);

}


int main () {
int a = 16;
int *p;
int **r;
p = &a;
r = &p;
int **movingresult;

printf("Value of p before moving :%p\n",p);
movingresult=moveforward(r);
printf("Value of p after moving :%p",movingresult);

return 0;

}

I just changed my code , so now it looks like this, everything runs well , but the result is not what i expected.

#include <stdio.h>

void moveforward(int** y) {
 *y = *y+1;
 printf("Value of *y now has been moved to : %p\n",*y);

}


int main () {
int a = 16;
int *p;
int **r;
p = &a;
r = &p;
int **movingresult;

printf("Value of p before moving :%p\n",p);
moveforward(r);
printf("Value of p after moving :%p\n",r);

return 0;

}

OUTPUT :
Value of p before moving :0x7fffa5a1c184
Value of *y now after moving : 0x7fffa5a1c188
Value of p after moving :0x7fffa5a1c178

MY EXPECTATION : the 'p' after moving must be equal to *y which is 0x7fffa5a1c188

7
  • 3
    moveforward is declared void. Commented Mar 22, 2017 at 1:26
  • I'm getting this error when I compile it. main.c: In function 'main': main.c:19:13: error: void value not ignored as it ought to be movingresult=moveforward(r); ^ exit status 1 Commented Mar 22, 2017 at 1:40
  • @KenY-N I have changed my code above. Commented Mar 22, 2017 at 2:08
  • printf("Address of p after moving :%p\n",r); --> printf("Address of p after moving :%p\n", p); or printf("Address of p after moving :%p\n",*r); Commented Mar 22, 2017 at 2:11
  • 1
    Why are you talking about "address of p" changing? You mean, the value of p (which is an address). Commented Mar 22, 2017 at 2:14

2 Answers 2

2

when you call the function moveforward, the pointer address of r not change. moveforward(r); after call moveforward, the address of r isnot change yet. so if you execute printf("Value of p after moving :%p\n",r);

the address of r are still not change.

if you want get your expected value, you should do: printf("Value of p after moving :%p\n",*r);

Sign up to request clarification or add additional context in comments.

Comments

1

This is how things look before doing any pointer arithmetic.

This is how things look before moving the pointer

After you execute moveforward(r);, this is how things look:

After moving the pointer

and these are the pointer values:

printf("Address pointed by p :%p\n",p); // Prints 0x7fffa5a1c188
printf("Address pointed by p :%p\n",*r); // Prints 0x7fffa5a1c188
printf("Address pointed by r :%p\n",r); // Prints 0x7fffa5a1c178

This is because, when you execute the following piece of code,

void moveforward(int** y) {
*y = *y+1;
printf("Value of *y now has been moved to : %p\n",*y);
}

Only the content of pointer p is incremented(0x7fffa5a1c184 to 0x7fffa5a1c188). But the content of pointer r (0x7fffa5a1c178) remains the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.