I'm recently reading about pointers in C but stuck at a point. I need to provide an example to make the question more clear.
I have declared two integer variables as a and b and assigned values 55 and 88 to them. Now by using the & symbol I can access their address and declare pointers as follows:
#include <stdio.h>
int main(void)
{
int a = 55;
int b = 88;
int *pa = &a;
int *pb = &b;
printf("Address of 'a' is: %p\n", &a);
printf("Address of 'b' is: %p\n", &b);
printf("Desired new address for the value of 'b' is: %p\n", (pa+1));
//What to do here to assign the value of b into the address (pa+1) ?
}
What I try to achieve(without using any arrays) is that: I want the value of b to be assigned to the address next to a.
I can obtain the address of a by &a or *pa = &a and the next address would be (pa+1). Now the pointer pa holds the address to the variable a. And so the value of (pa+1) I guess should point the address of the next possible address of a.
Now my problem is having the value of b and the address (pa+1) how can we register/assign the value of b to the address (pa+1)?
edit:
Following doesn't work:
int a = 55;
int b = 88;
int *pa = &a;
int *pb = &b;
printf("Address of 'a' is: %p\n", &a);
printf("Address of 'b' is: %p\n", &b);
printf("Desired new address for the value of 'b' is: %p\n", (pa+1));
*(pa + 1) = b;
printf("New address of 'b' is now: %p\n", &b);
Outputs:
Solved:
int main(void)
{
int a = 55;
int b = 88;
int c;
int *pa = &a;
int *pb = &b;
printf("Address of 'a' is: %p\n", &a);
printf("Address of 'b' is: %p\n", &b);
printf("Desired new address for the value of 'b' is: %p\n", (pa+1));
*(pa + 1) = b;
printf("New value at pa+1 is now: %d\n", *(pa+1));
}

b. This is not possible. The address of any variable is decided by the compiler and cannot be changed. If a is next to b you cannot simply squeeze another value in between. You can only ever assign a new value via an address but you can never change this address.bhas a lower address thana.