Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Address conversion

Status
Not open for further replies.

JackMorris

Technical User
Jan 30, 2003
18
IE
I would like to do following things:

1. Allocate the memory to store 10 integer values.
2. Store the 10 random values in the allocated memory.
3. Get the address of the first value stored. ( The address will be in hexadecimal).
4. Convert the hexadecimal address value into decimal value.
5. Add 4 in the calculated decimal value of the address.
6. The sum should be the address of the second value stored (since the integer occupies 4 bytes) in the allocated memory. The address is still in the decimal value.
7. convert the decimal value into the hexadecimal.
8. Print the value in this address ( as I said earlier this should be the second value in the allocated memory).

Could anyone suggest me how the conversion thing over here could be performed correctly such that I get the right content of the second value in the allocated memory space, if I follow the steps i mentioned above.

thanx in advance.
Jack
 
You don't need to do any conversion. Hex / Decimal is only invented because of human eyes/brains : It's the same number hidden behind in the 32 bits.

An address is an address. (Period !).

Add 4 to the address returned and you will have the next item.

/JOlesen

 
Just trying to solve your next problem ;-) :

If you add a number to a pointer, the pointer will be incremented with a value of number * sizeof(object).

If it's an integer pointer and you add 4 :
The pointer will point to the 4. item.
Address will be increment with 4 * sizeof(int).

That's why people often convert a pointer to a char pointer before doing math on the pointer (ie when the programmer don't want the compiler to be that 'intelligent').

/JOlesen

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top