Skip to content

Learn the basics of pointers

Posted on:August 1, 2023 at 11:22 AM

While working in C, you might have heard about the concept of pointers. To some they seem scary, but they are innocent, if you know how to use them. To understand what are pointers, we need to know a bit about hardware.

Level 1

The CPU can store data in RAM, which can be either variables or code to be run(machine code). The RAM can be viewed as a large array of bytes that can be modified. Pointers are addresses that point to bytes in RAM.

For example, you can get the address of a static variable using:

int main()
{
    int p = 2;
    int* k = &p;
}

You can also dereference, get the value of a variable at the specified address. It can be performed as such:

int value = *pointer;

Memory can be allocated dynamically, for example, when you want an unknown sized or variable length array. In C you can use malloc and free:

int* p = (int*)malloc(4 * sizeof(int)); // allocates 4 * number of bytes in int = 4 integers
p[0] = 1;
p[1] = 10;
free(p);

In C++ you may be familiar with the new or delete operators. Using them, you can allocate memory:

int* p = new int[4];
p[0] = 1;
p[1] = 10;
delete[] p;

You use delete[] if you have allocated an array pointer. Else, you use delete. Don’t mix C memory allocation with C++(new with free or other combinations), they are not compatible. As you can see, these types of pointers can be accessed like arrays, because pointers can be addresses to arrays.

Level 2

Strings in C are just array of characters with a null character at the end. They don’t have an inherit length as in Java, C# or others. If you are not careful, you could end up in segfault, usually if you use scanf.

Also, in string.h there are functions that can manipulate strings. For example, strlen(s) gives the length of a string and strcmp(s1, s2) compares the two strings. If the result is zero, they have the same contents, if not, it returns a number based on the difference of characters(ASCII code).

strtok(s, delim) gives the first substring cefore the delimitator. You can use strtok(NULL, delim) in a while loop to get all the tokens.

Level 3

We saw in the first section how you can dereference a pointer. Well, the truth is you can’t dereference any pointer. If you do *NULL it will cause a Segfault, but also unmapped pointers, Doing this:

int* p = (int*)0xA12345;
printf("%i", *p);

is not the great idea. Because you don’t access physical memory, but rather virtual. Virtual memory can be mapped either to physical memory or to swap files(regions in hard disk). You can use mmap to manually do this:

mmap(p, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0);

It uses -1 for filedes, as it doesn’t map files to memory. Also, as seen in the second section, you can use memory manipulation functions, but what if I tell you they are not very safe. They can be susceptible to buffer overflows, but there are safe versions to it. They are the _s suffixed functions, like strlen_s.