Storing values, internal mechanisms?
Hi all
I got a silly question...I actually came to this "problem" when looking at endianess.
I have the following test-code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *out = fopen("test.txt", "w");
char test[] = "this is a test!\n";
int *p = malloc(sizeof(int));
memcpy(p, test, sizeof(int));
printf("%x\n", *p);
fwrite(p, 1, sizeof(int), out);
fclose(out);
return 0;
}
So. The value of "p" gets printed in hex:
This would match: siht, so "this" spelled backwards. Its a little endian machine, so small-end stored first, therefore it's 73 ('s') that should be the biggest value and is printed first when interpreted as an integer.
But the following rest remains a riddle...
The code above also writes a file "test.txt" and stores the value of "p". Hexdump reveals:
Quote:
0000000 6874 7369 -->matches "htsi"
0000004
But when reading the file "byte by byte" with "fgetc(...)" and output it hex I get:
which would match "this" again.
So there are 3 different forms now. Why ? I don't get it...
Anybody an idea? ;)