• Home
  • Pointer Basics – Array Interpretation

Pointer Basics – Array Interpretation

View Categories

Pointer Basics – Array Interpretation

< 1 min read

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 + 1 moves by sizeof(int) bytes
  • cptr + 1 moves by 1 byte

The output order depends on system endianness. On most modern systems (little-endian), the least significant byte appears first in memory.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *