'Reinterpretation of memory

I have been googling a lot on this and have been reading a lot, but still I am not quite understanding it. I hope someone here could cite me a sample with detail explanation.

What is "reinterpretation of memory"?

Let say I have:

char *myChar;

int myInt = 5;

//Or

float myFloat = 5.5;

And I would like to change myChar so it points to, let say, an int or float like the above.

How could I make use of "reinterpretation of memory" to make this work so that myChar can point to either myInt or myFloat?

Again, what does "reinterpretation of memory" mean?

Edit:

What I really want to do is instead of casting either int or float to char pointer, but to actually change myChar to be of type int or float pointing to either myInt or myFloat.

I.e.:

<cast?> myChar = &myInt; //or &MyFloat;


Solution 1:[1]

mychar = (char *)&myInt;

"Reinterpretation of memory" heremeans that you are treating the bytes that contain the representation of an int, as if they contained representation of chars.

The C standard talks about which types you are allowed to do this on. You can treat any object as if it contained chars, for reading purposes, however in most cases you can't portably write back to it, and you can't portably do it for two non-char types.

Solution 2:[2]

char* myChar;

for simplicity lets assume big endian:

                       memory
                 +----+----+----+----+
int myInt = 5;   | 00 | 00 | 00 | 05 |
                 +----+----+----+----+

the integer value 5 i placed in a memory slot big enough to hold an int

you can access each of those four bytes by setting using the char* myChar and pointing to the first byte (since myChar has size 1)

myChar = &myInt;

with myChar you are in effect interpreting the memory of myInt as a series of bytes (chars) through the myChar pointer.

lets also do an integer pointer as comparison

int myPtr = &myInt;

You have now an integer pointer to the integer.

The data type of the pointer that accesses the memory determines the behaviour of the pointer:

myChar + 1 means the next byte (00)
myPtr + 1  means the next int (if myInt where an array of ints)

Solution 3:[3]

To be blunt, the term "reinterpretation of memory" sounds like a political liberal trying to re-invent history. From the perspective of C programming, it sounds like someone is trying to make-up a new term to impress their students with their grammatical command of language.

Most likely, it is to do with casting, or unions; both of which allow memory content to be interpreted different ways.


QUESTION EDIT: What I really want to do is instead of casting either int or float to char pointer, but to actually change myChar to be of type int or float pointing to either myInt or myFloat.

Perhaps try a union?

union 
   {
   void  *myVoid
   char  *myChar;
   int   *myInt;
   float *myFloat;
   } myPtr;

Then to use it:

char character = 'x';
int  integer   = 12345;
float floatie  = 3.1415926535;

Then:

myPtr.myChar = &character;
printf("myChar: %c\n", *myPtr.myChar); // Output: "myChar: x"

or

myPtr.myInt = &integer;
printf("myInt: %d\n", *myPtr.myInt); // Output: "myInt: 12345"

or

myPtr.myFloat = &floatie;
printf("myFloat: %f\n", *myPtr.myFloat);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M.M
Solution 2 semisdot
Solution 3