7

I have a code that I passed a certain place in the memory. This place in the memory is pointing to an array

uint32_t *ps2 = NULL;
uint32_t src_address = 0x1ffffc3;

How can I read the value of the array from this address? I tried to cast it as follows

*ps2 = (void *)src_address;

but it gives me an error: invalid conversion from ‘void*’ to ‘uint32_t

Regards,

6
  • What type are the varables stored inside that array? Is this an array of 32-bit unsigned integers stored in native machine endianess? Commented Apr 4, 2019 at 8:33
  • Thanks friends, Basically I have the address that I need to access in the src_address and I need to convert it so I can access its content. Commented Apr 4, 2019 at 9:25
  • I tried also to deifne uint32_t ps2value = 0; uint32_t *ps2 = &ps2value; it also gives me the same error. Commented Apr 4, 2019 at 9:28
  • Basically I have the address that I need to access in the src_address and I need to convert it so I can access its content address is address. You can convert the type of the variable, not the address. You have an address uint32_t src_address. You can convert the type to uint32_t *src_address_pnt = (uint32_t*)src_address. If your compiler is really pedantic, try uint32_t *src_address_pnt = (uint32_t*)(void*)(uintptr_t)src_address. Also please post full exact error message, include relevant compiler and compiler options. Commented Apr 4, 2019 at 9:40
  • warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] ps2 = (uint32_t *)src_address; // ^~~~~~~~~~~ error: invalid conversion from ‘uint32_t {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ [-fpermissive] *ps2 = (uint32_t *)src_address; ^~~~~~~~~~~~~~~~~~~~~~~ Commented Apr 4, 2019 at 10:20

4 Answers 4

8

You have two problems:

  1. First of all, the pointer ps2 is a null pointer, it doesn't point anywhere. That means you can't dereference it.

  2. src_address is not a pointer, when it really should be.

All in all there's seems to be some mixup in your understanding of pointers and how they are used.

For it to work, first define ps2 as not a pointer:

uint32_t ps2;

then define src_address as a pointer:

uint32_t *src_address = (uint32_t *) 0x1ffffc3;

and finally dereference src_address like a normal pointer:

ps2 = *src_address;

There is a possible third problem: The address of src_address is not aligned for an uint32_t. On some systems unaligned access is invalid and will lead to hardware exceptions.

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

2 Comments

I tried this but it is not working also error: invalid conversion from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}
@asd That looks like you forget to dereference the pointer on on the right-hand side of the assignment.
0

This is because you cannot guaranteely convert a pointer type to any integer type. 6.3.2.3(p5) (emp. mine):

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

The undefined behavior may well be the compile error you see.

There are dedicated types intptr_t, uintptr_t. They are described at 7.20.1.4

The following type designates a signed integer type with the property that any valid pointer tovoidcan be converted to this type, then converted back to pointer tovoid,and the result will compare equal to the original pointer

If your implementation implements the types you should use them. If no there is no any other conforming portable way to convert integers to pointers.

Comments

0

You want to give pointer p2 while (other than NULL).

But here you set this value not to pointer itself, but to memory it points to.

*ps2 = (void *)src_address;

And it points to....well nothing, it's NULL pointer (address 0 is not valid).

By using * you are accessing (or setting) value that pointer points to. So you need to remove * to change the pointer itself.

ps2 = (void *)src_address;

Or even better:

ps2 = (uint32_t*)src_address;

Then to read value from that address:

uint32_t value = *ps;

Comments

0
uint32_t *ps2 = NULL; // Assuming this is where you want your array to point.
uint32_t src_address = 0x1ffffc3; // source array

You can do :

ps2 = (uint32_t *)src_address; // Assuming 32 bit machine

Now to access every element of array, all you have to do is -

*ps2[0], *ps2[1] 

ps2[0] will point to src_address + 0. ps2[1] will point to src_address + 4(As pointer type is uint32_t)

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.