Treat a variable value as an address and assign this value to a pointer

0

I encounter a problem when I try to treat a variable value as an address and assign this value to a pointer.

Here are what I did so far.

  1. declare a pointer uint32_t *p;
  2. declare and initialize two variables

    uint32_t a = 0x0000000C; uint32_t b = 0x0C00000B;

  3. initialize the pointer p = &a;

  4. now I want to treat variable b's value which is 0x0C00000B as an address, and put this to p. in this way pointer p now will pointer to address 0x0C00000B. but somehow it doesn't work.

Here were the thing I tried .

p = b;   doesn't work. 

p = (uint32_t *)b ; doesn't work .

After compilation, there is one warning :

warning: incompatible integer to pointer conversion assigning to 'uint32_t ' (aka 'unsigned int') from 'uint32_t' (aka 'unsigned int'); take the address with & [ -wint- conversoin.

The problem is the the pointer p will be randomly assigned with some number instead 0x0C00000B

Any suggestions?

c
asked on Stack Overflow Jun 7, 2019 by Xiao Wen • edited Jun 7, 2019 by Russ Schultz

3 Answers

1

The "most correct" way convert between an integer to a pointer without any warnings, you have to cast to uintptr_t, then to void*, then to your pointer type.

uint32_t b = 1;
uint32_t *p = (uint32_t*)(void*)(uintptr_t)b;

But usually programmers just do macros, that are properly cast:

#define A_ADDR  ((uint32_t*)0x0000000A)
#define B_ADDR  ((uint32_t*)0x0000000B)
uint32_t *p = A_ADDR;
p = B_ADDR;

which compiles without a warnings on gcc -Wall -Wextra -pedantic.
Or sometimes just macros with constant numbers, and the user is supposed to cast to (void*) or to proper type:

#define A_ADDR 0x0000000A
#define B_ADDR 0x0000000B
uint32_t *p = (uint32_t*)A_ADDR;
p = (uint32_t*)B_ADDR;

Alternatively, if you trust your compiler, they can be static const variables, ex. static const uint32_t *A_ADDR = (uint32_t*)0x0000000A;

answered on Stack Overflow Jun 7, 2019 by KamilCuk
0

I understand your problem like below.

You will store the address of a memory location in a variable and assign this to a pointer.

In your example, declare a pointer uint32_t *p; declare and initialize two variables uint32_t a = 0x0000000C; uint32_t b = 0x0C00000B;

If you want to know the value at address 0x0000000C , you need to assign a to ptr. like,

p = a; not like p = &a;

which will store the address of a in the pointer variable. which is not correct as per your problem statement.

answered on Stack Overflow Jun 7, 2019 by Dineshkumar
0

You already have the solution - p = (uint32_t *)b ; doesn't work . The above line treats the value in variable 'b' as an address, and assigns that address to pointer 'p'. But you may get some compiler warnings. From my understanding of your use-case, p is to be used as a tracker to some location in memory. If so consider writing something similar to this, to not produce any warnings -

#define TRACKER_BASE_ADDR  (0x0C00000B)
#define TRACKER_OFFSET     (0x04)
uint32_t *p = TRACKER_BASE_ADDR;
// When you want to update the tracker use this
p = p + TRACKER_OFFSET;
// And when you want to read the value at tracker,
uint32_t val = *p;

Hope this helps

answered on Stack Overflow Jun 7, 2019 by SRK

User contributions licensed under CC BY-SA 3.0