← All posts
November 26, 2026 · 2 min read

Dereferencing a pointer in C++: what *p reads, byte by byte

Dereferencing a pointer in C++ reads or writes the bytes at its address; the type says how many. One real run: *p reads 4 bytes, *q reads 1.

Dereferencing a pointer in C++ means taking the address the pointer holds and reading or writing the bytes at that address. The * in *p does it. What the address does not say is how many bytes to touch or how to interpret them; that comes from the pointer's type. This walks through one real run of a short program: an int, an int* to it, a write through the pointer, and an unsigned char* to the same address that reads something different.

Everything below comes from one run of the program at the end, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) on x86-64 Linux. Stack addresses change on every run, so yours will differ; the bytes and the sizes will not.

What is in memory before any pointer?

int x = 42; is 4 bytes. 42 is 0x2a, and x86-64 stores the lowest byte first, so the bytes are 2a 00 00 00. On this run they start at 0x7ffd8212f754.

A hex dump of one stack row: int x = 42 is the bytes 2a 00 00 00, starting at address 0x7ffd8212f754

The picture shows memory 8 bytes a row, starting at 0x7ffd8212f750. The four bytes before x belong to something else and were not printed, so they are drawn as dots.

What does *p read?

int* p = &x; stores x's address in p; the program prints p as 0x7ffd8212f754. *p goes to that address and, because p is an int*, reads sizeof(int) = 4 bytes and treats them as an int: 2a 00 00 00, which is 42.

int* p holds 0x7ffd8212f754, an arrow points to x's first byte, and a bracket under the 4 bytes shows *p reads 4 bytes: 42

What does *p = 300 do?

*p also works on the left of =. *p = 300; writes 4 bytes at the address in p. 300 is 0x12c, so lowest byte first the bytes become 2c 01 00 00.

After *p = 300 the same 4 bytes are 2c 01 00 00: 300 is 0x12c, stored lowest byte first

The program never assigns to x after initialising it, yet printing x now gives 300. There is only one set of 4 bytes; x and *p are two names for them.

step bytes at 0x7ffd8212f754 (lowest first) read as int
int x = 42; 2a 00 00 00 42
*p = 300; 2c 01 00 00 300

Why does *q read a different number from the same address?

unsigned char* q = (unsigned char*)p; copies the same address into a pointer of a different type. The program prints q as 0x7ffd8212f754, identical to p. But sizeof(unsigned char) is 1, so *q reads one byte: 0x2c, which is 44.

unsigned char* q holds the same address, but *q reads one byte, 0x2c, which is 44; the whole program deref.cpp is below

So the address says where the bytes are, and the type says how many bytes to read and what they mean. Reading any object's bytes through an unsigned char* like this is allowed in C++; it is how you inspect an object's representation.

The program and its output

#include <cstdio>
int main() {
    int x = 42;
    int* p = &x;
    printf("p  %p\n", (void*)p);
    printf("*p %d\n", *p);
    *p = 300;
    printf("x  %d\n", x);
    unsigned char* q = (unsigned char*)p;
    printf("q  %p\n", (void*)q);
    printf("*q %d\n", *q);
}
p  0x7ffd8212f754
*p 42
x  300
q  0x7ffd8212f754
*q 44

The bytes of x before and after the write (2a 00 00 00, then 2c 01 00 00), sizeof *p = 4 and sizeof *q = 1 were checked with a second program that copies x's bytes out with std::memcpy and prints them.

Try it

After *p = 300, q + 1 is the address of the next byte of x. What is in that byte? Work it out from the four bytes above, then add printf("%d\n", *(q + 1)); to the program and check.