C Programming/Pointers

From Wikiversity
Jump to navigation Jump to search

Objective[edit | edit source]

  • Learn about pointers and how to use them.
    • Declaring pointers.
    • Dereferencing pointers.

Lesson[edit | edit source]

Introduction[edit | edit source]

Pointers refer to memory addresses, as opposed to specific values. They are denoted by the use of an asterisk (*). Consider the following piece of code:

int i = 1;
int *k = &i;
int m = i;
printf("i (%p): %d\n", &i, i);
printf("k (%p): %d\n", k, *k);
printf("m (%p): %d\n\n", &m, m);
printf("i (%p): %d\n", &i, i);
printf("k (%p): %d\n", k, *k);
printf("m (%p): %d\n", &m, m);


i (0x7fff62b5fa54): 1
k (0x7fff62b5fa54): 1
m (0x7fff62b5fa50): 1

i (0x7fff62b5fa54): 2
k (0x7fff62b5fa54): 2
m (0x7fff62b5fa50): 1


In this code, k is set to the address of i, where m is set to the value of i. When i is incremented, the change is shown in k, since they both refer to the same memory address. m stays the same, however, since it took the value once, and stored it in a separate memory location.

In the brackets can also be seen the memory address of the variable, which shows that i and k refer to the same memory address, while m refers to a different memory address. Note that in the example, the following are true:

  • We used int * to declare a pointer to an integer
  • We used &i to get the memory address of i
  • We used *k to get the value stored in the address referred to by the pointer k

Pointers as Array Iterators[edit | edit source]

Pointers can be incremented, which make them a natural choice for iterating an array. Consider the following piece of code:

int a[5] = {0,2,5,8,11};
int *ptr = &a[0];
for (int i = 0; i < 5; i++) {
        printf("%p: %d\n", ptr, *ptr);


0x7fff645ffa40: 0
0x7fff645ffa44: 2
0x7fff645ffa48: 5
0x7fff645ffa4c: 8
0x7fff645ffa50: 11


ptr initially refers to the first item in the array, a[0]. When ptr is incremented, the memory address is in fact incremented by sizeof(int). The value at the address referred to by ptr is therefore then the next value in the array, accessed by *ptr.

Special Application: Linked Lists[edit | edit source]

A linked list is a list of data, comprised of nodes which specify their data, and the address of the next node, or NULL to specify the last node. Consider the following code:

#include <stdio.h>
#include <stdlib.h>

struct Node {
        int data;
        struct Node * next;

int main() {
        struct Node * head = malloc(sizeof(struct Node));
        head->data = 0;
        struct Node * curr = head;
        for (int i = 1; i < 5; i++) {
                struct Node * new = malloc(sizeof(struct Node));
                new->data = 5*i;
                curr->next = new;
                curr = curr->next;
        curr = head;
        while (curr != NULL) {
                printf("%d\n", curr->data);
                curr = curr->next;
        return 0;

In this code, we defined a struct for a node in the linked list. We initialize the first node, and then add some data, by creating a new node, setting it to be after the current node, and then moving the current node to the next place. We do a similar process to iterate through the list.

A linked list is a good visualisation of pointers, but is also an effective data type for Last in First Out structures, such as stacks. Adding to a linked list is simply a matter of creating a new node, and setting its next to be the head. Removing is equally simple, get the value, deallocate the memory, and set the second node to be the head.

Special Application: Function Pointers[edit | edit source]

In addition to other ways that pointers can be used, they can also reference a function. While this may not seem very useful at first, it can be used to either embed a function in to a struct that uses it exclusively, or to allow a function to return a function or use a function as input to another function.

This is a very complex sub-topic and can create a lot of bugs in your program if poorly implemented.

Assignments[edit | edit source]

Completion status: About halfway there. You may help to clarify and expand it.