Basically how can I do it?

The values I want to read (going to be writing to file later) are hex pairs meaning the values are stored across more than one address location.

The first value is stored at pointer address 12feac, the next pointer is at 12feb0

So reading the address 12feac won't provide the value I'm after as its stored from 12feac, 12fead, 12feae and 12feaf.

I'm a novice so be gentle :)


6 Years
Discussion Span
Last Post by dinohead

Dereferencing a pointer does not just read the byte that the pointer directly points to - it also reads the subsequent bytes depending on how large the pointed-to data type is. So if ptr is a variable of type T* that points to the address 12feac and sizeof(T) is 4, then writing *ptr will read the bytes 12feac, 12fead, feae and 12feaf. So everything's fine.


I get what you are saying but I cant get it to work.

I think I have it wrong. Here's my code - its not printing out my expected value

            sizeof(value) == 4;
            value = *ptr; 

            printf("The data read = %x \n", value);  /* Printing the value */

You need to post a self-contained code-sample that compiles, runs and demonstrates your problem. I can't help you just from looking at those 2 lines of code.


where is the declaration of value? and ptr?
without knowing these, i am going to make assumptions.
so correct me as necessary...

you seem to be using c, and not c++,

  1. how do u plan to read the 4 bytes?
    do they compose 1 larger value (an integer, by the way, coz it's 4 bytes)
    or is it an array of 4 characters?

IF it is an integer, so long as your pointer is declared as int*, you should be fine.

int * ptr = (int*) 0x12feac;
printf("The value addressed by ptr is %i", *ptr);

if you want to be a little more specific for the compiler, use

int * ptr = (const int) 0x12feac;
printf("The value addressed by ptr is %i", (int
) ptr);

note, however, that if you do not 'own' the 0x12feac,
the program will issue an error (segmentation fault probably).

  1. and please remove: sizeof(value) == 4;
    it DOES NOT assign a size of 4 bytes to value.
    u can do that by its declaration

int value = 2131 ;// any number will do

the FACT that it is DECLARED as INT gives it 4 bytes.

(err i may have misunderstood u here, so explain ur point if i did)

Edited by karlsiegfried.lizaso

Votes + Comments

Wow thanks awesome reply.

seems that within the 4bytes is a hex pair. For example - FF so instead of an int isn't it an unsigned char? Or will that not allow reading from an address range

Oh and yes its ansi c

Edited by dinohead


So the test data is stored here

*hex = 0x0F; 
    *(hex+1) = 0x11; 
    *(hex+2) = 0xFF;
    *(hex+3) = 0x1B;
    *(hex+4) = 0x00;
    *(hex+5) = 0xB6;

And this is created using the malloc block

hex = (unsigned char *)malloc(0x65536);

The starting memory address of hex starts at 12feac (*hex) and *hex+1 is 12feb0

I just can't successfully read the value - which I need to figure out as a starting point as later I want to write the value to file



Any ideas?

I think once this is cracked I can finish it off

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.