This example shows how the same block of memory can be interpreted differently depending on the pointer type used. Arrays are stored as contiguous bytes in memory, and pointer types decide how those bytes are accessed.
main.c
Copy to clipboard
#include <stdio.h>
int main()
{
int arr[] = {0x11223344, 0x55667788};
int *iptr = arr;
char *cptr = (char *)arr;
printf("*iptr = %x\n", *iptr);
printf("Array interpreted byte-by-byte:\n");
for (int i = 0; i < sizeof(arr); i++)
{
printf("%x ", *(cptr + i) & 0xFF);
}
printf("\n");
return 0;
}
Output:
*iptr = 11223344
Array interpreted byte-by-byte:
44 33 22 11 88 77 66 55
iptr is an int *, so dereferencing reads the array as integer values.
cptr is a char *, so memory is accessed one byte at a time. This allows viewing the raw byte representation of the array.
Arrays are stored continuously in memory. Pointer arithmetic depends on the pointer type:
iptr + 1moves bysizeof(int)bytescptr + 1moves by 1 byte
The output order depends on system endianness. On most modern systems (little-endian), the least significant byte appears first in memory.