0

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:

enter image description here

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));

}
13
  • It seems you want to assign a new address to 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. Commented Nov 22, 2019 at 16:09
  • No I want to assign the "value of b" to the address pa+1 Commented Nov 22, 2019 at 16:10
  • The question explicitly says "value of" Commented Nov 22, 2019 at 16:10
  • 1
    But you print says "Desired new address for the value" Commented Nov 22, 2019 at 16:11
  • Are you aware that local variables in functions are likely to be stored on the stack in order of decreasing addresses? b has a lower address than a. Commented Nov 22, 2019 at 16:12

3 Answers 3

1

To write a value to an address, you use *address = value;.

For example,

*(pa + 1) = b;

However, by doing this, you are very likely to overwrite some piece of memory that was supposed to be used for something else, and crash your program.


Your edit to the question makes it apparent that you want to change the address of a variable. This is impossible. If it was possible, it would be written like &b = pa + 1;.

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

1 Comment

@floppy380 Updated.
0

To write the value of b to the address you want you need to use:

*(pa + 1) = b

But bare in mind that this causes undefined behaviour so you may get what you want or you may not, no way of knowing for sure.

This way you will write the value but the address of b won't change, that's why in your print you don't see any difference.

3 Comments

I want to assign the "value of b" to the address pa+1 not the b itself. Maybe I should use int c = b; and then use c
@floppy380 you are assigning the value of b... try to print *(pa + 1) and you see that you will find 88 (if it doesn't crash due to undefined behaviour)
@floppy380 the value of b is 88, the address of b is 0060FEF0 or whatever your program puts the variable into.
0

You have (pa+1) which is an address of type int *, so all you need to do is dereference it:

*(pa+1) = b;

You can then subsequently print that value like this:

printf("valud=%d\n", *(pa+1));

Note however that what you're doing, writing past the memory bounds of an object, is not allowed by the C standard and in fact invokes undefined behavior. Such code is only useful when attempting to execute an attack such as a buffer overflow.

6 Comments

@floppy380 Please include an explanation of exactly what you are trying to accomplish and what the expected behavior is, keeping in mind the undefined nature of what you are doing.
I want to write the value of b to an adress I define. In this case to the adsress pa+1
@floppy380 Which this code does. Why do you think the address of a variable would change just because you wrote the value it contains elsewhere?
Im working on it just a minute I want to assign the value of b not the b. Maybe I should use int c = b; and then use c
@floppy380 You need to be more clear on exactly what you're expecting. Perhaps you want to read `*(pa+1)? Also, please detail why you are doing this in the first place and what you hope to accomplish by doing so. It may lead to a better answer.
|

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.