Skip to content
thesarfo

Note

C: Pointers

What a pointer actually is, memory addresses, and the address-of operator.

views 0
#include <stdio.h>
void main(void){
printf("a long uses %d bytes of memory\n", sizeof(long));
}

Each byte of computer memory is identified by an integer. These integers increase sequentially as you move up through memory. Each integer is called an address.

Not all datatypes use just a byte — you can use the sizeof() operator to check the bytes a type uses.

When you have a data type that uses more than a byte of memory, the bytes that make up the data are always adjacent to one another in memory. Sometimes they’re in order, and sometimes they’re not, but that’s platform-dependent, and often taken care of for you without you needing to worry about pesky byte orderings.

A pointer is simply the address of some data in memory.

So let’s say you have an int, and you want a pointer to it — what you want is some way to get the address of that int, since the pointer is just the address of that int. You can use the “address-of” operator (&) to find the address of the data.

Just like you have the format specifier %d to print a decimal int, %p prints a pointer. This pointer looks like a garbage number and will be in hexadecimal format. See below on how to print a pointer of an int:

int another(void){
int i = 10;
printf("The value of i is %d, and its address is %p\n", i, &i);
return 0;
}