im very new to programming so i need some help.
Im currently trying to read the memory directly using pointers,
for example 23 stored in the memory for a float variable is: 41b80000
is there any way i can directly read this value from the memory and store it in an array?
Note: I tried to create a pointer which points to the memory location but when i dereference it, it gives me 23.
any ideas would be GREATLY appreciated, ive been working over this for almost 5 hours now.

Recommended Answers

All 4 Replies

This code shows creating an int, creating a pointer to it, creating an array of pointers, storing the memory address as a plain int, and then using that plain int to get back the contents of the memory address.

It's C++ code to make outputting the values easy; the important code manipulating the pointers is plain C. If you've not got a C++ compiler to hand, it's here as well:


#include <iostream>

int main()
  int someInt = 7;
  int* p = &someInt;

  // p now contains the address of someInt
  // You could store it in an array if you like, as follows
  int* pointerArray[4]; // make an array of int pointers

  std::cout << p << std::endl;
  std::cout << pointerArray[0] << std::endl;

  // now turn that memory address into a plain int
  int memoryAddress = (int)pointerArray[0];
  std::cout << std::hex << memoryAddress << std::endl;

  // It's a plain int. You can store it in an array if you like

  // Now use that plain int to get the original someInt value
  int* q = (int*)memoryAddress;
  std::cout << *q << std::endl;

  // or directly
  std::cout << *(int*)memoryAddress << std::endl;

  return 0;

Thanks ALOT Moschops
just one more thing, what does this line mean :
int* q = (int*)memoryAddress; ??
according to me, an integer pointer q is equal to original value of memory address?
more like what does the symbol (int*) mean
sorry for sounding like a complete newbie
and thanks again

q is an int pointer but memoryAddress is an int. It is nonsensical to set the value of an int pointer to that of an int; you must set the value of a pointer to equal another pointer, or an explicit address (as I did with the line int* p = &someInt;). However, because we know that the value of that int makes sense if interpreted as an int pointer, we can instruct the compiler to do so.

(int*)memoryAddress instructs the compiler to take the variable memoryAddress and treat it as if it were an int pointer. This is known as casting. You cannot rely on the compiler to stop you and it is up to you to ensure that it is sensible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.