Embedded Development Board Learning


Pointers

Date: agosto 25, 2025

Author: Guillermo Garcia

Categories: Embedded C programming Tags:

In this article we are going to look at one of the most powerful qualities of the C language, which are pointers. This quality makes the C language ideal for embedded systems.

Pointer principle

In C, every variable has a type, an address in memory and a value. A pointer is a variable whose value is the address of another variable. Hence, you can say that the pointer “points” to that other variable.

poiter example in C

Declaration

Like every variable in C, a pointer variable must be declared before it can be used. In its most general form, a pointer declaration can be written as:

<target_datatype> * <name>;

The * in the declaration indicates that the variable is a pointer. For instance

int *ptr;

declares the pointer variable with name ptr that can point to a variable of the type int.

When a pointer is declared, memory is reserved to store an address only! No memory is reserved for the variable the pointer will refer to, nor does the pointer point to any variable yet!

Pointers should be initialized when defined or they should be assigned a value. Pointers can be initialized to NULL or an address. A pointer initialized to NULL is a pointer that points to nothing.

Address and dereference operator

Now that the pointer is declared and initialized, we still need to make sure it points to the variable of our choice. To this end, the address of that variable must be assigned to the pointer.
To determine the memory address of a variable, the address operator & is used:

int *p = NULL; //declaration and initialization of the pointer
int a = 5; //declaration of the integer variable a
p = &a;

The statement “p = &a;” assigns the address of the integer variable a to the pointer p. As a result, the pointer p now points to the variable a.

On the other hand, it must also be possible to read the value of the variable the pointer is pointing to. The operator to be used is called the dereferencing operator and is represented by the symbol *.

int *p = NULL; //declaration and initialization of the pointer
int a = 5; //declaration of the integer variable a
p = &a;
printf(“%d”, *p);

The statement “printf(“%d”, *p);” will output the number 5 to the screen. *p performs the dereferencing operation on the pointer p. It reads the address stored in the pointer variable p, goes to that memory location and returns the value stored at that location.
The same operator can be used to store values into the variable the pointer refers to:

*p = 8;

When above statement is executed, the address stored in the pointer variable p will be read and the number 8 will be stored at that memory location.

Example

The example demonstrates the address and the dereferencing operator:

#include <stdio.h>

int main(void)
{
 int a = 5;
 int *p = NULL;
 p = &a;
 printf("The address of the variable a: %x\n", &a);
 printf("The content of the variable p: %x\n", p);
 printf("The content of the variable a: %d\n", a);
 printf("The content of the variable p points to: %d\n", *p);
 printf("The address of the pointer variable: %x\n\n", &p);
 return 0;
}

Output:

The address of the variable a: 34faf8
The content of the variable p: 34faf8
The content of the variable a: 5
The content of the variable p points to: 5
The address of the pointer variable: 34faec

Passing arguments to functions

When calling functions that receive parameters, passing parameters can be done in two different ways: one is simply passing the value of the variable, which is called passing by value, and the other is passing as a pointer, which is called passing by reference.

Pass by value

If a function with parameters is called, the values of the arguments are copied to the function parameters. Inside the function, these parameters can change value. Once the function is finalized, the changes done to the function parameters are not automatically copied to the original variables used as arguments. If one of the changed values needs to be copied to the calling function, a return statement must be used.

Example

#include <stdio.h>
#define SIZE 10

int function(int, int[]);

int main(void)
{
 int i, n, res;
 int b[SIZE];

 n = 5;
 for (i = 0; i < SIZE; ++i)
 b[i] = i;

 printf("Before the function call: n = %d\n", n);
 printf("and the array b contains: ");
 for (i = 0; i < SIZE; ++i)
  printf("%4d", b[i]);

 res = function(n, b);

 printf("\n\nAfter the function call: n = %d,\n", n);
 printf("the array b contains: ");
 for (i = 0; i < SIZE; ++i)
  printf("%4d", b[i]);

 printf("\nand res = %d\n", res);

 return 0;
}

int function(int x, int y[])
{
 int i;

 x = x * x;

 for (i = 0; i < SIZE; ++i)
  y[i] = i * i;

 return x;
}

Output:

Before the function call: n = 5
and the array b contains: 0 1 2 3 4 5 6 7 8 9 

After the function call: n = 5
the array b contains:    0 1 4 9 16 25 36 49 64 81

In this example, the value of n is copied into the function parameter x. In the function, x is modified, but the variable n in the main function remains unchanged. The array b on the other hand is changed from within the function. In the case of an array, the starting address of that array is passed to the function. As a result the array in the function (y) and the original array (b) point to the same memory location!

Pass by reference

Often, the changes carried out on the parameters in the called function need to affect also the arguments in the calling function. To this end, a mechanism similar to passing arrays to functions needs to be used. Instead of passing the value of a variable to the function, the address of that variable needs to be passed to the function parameter. As a result, the function parameter is now a pointer that points to the original variable. So, through the pointer in the called function, the original variable can be altered.

Example

#include <stdio.h>

void read(int *, int *);
void swap(int *, int *);

int main(void)
{
 int a, b;

 read(&a, &b);

 printf("\nBefore the function call: a = %d and b = %d\n", a, b);
 swap(&a, &b);
 printf("After the function call: a = %d and b = %d\n", a, b);

 return 0;
}

void read(int *x, int *y)
{
 printf("Enter two integer numbers: ");
 scanf("%d%d%*c", x, y);
}

void swap(int *x, int *y)
{
 int temp;

 temp = *x;
 *x = *y;
 *y = temp;
}

Output:

Enter two integer numbers: 5 8
Before the function call: a = 5 and b = 8
After the function call:  a = 8 and b =5

The pointers x and y in the functions swap and read point to the original variables a and b. Therefore, a and b can be modified directly from inside the functions.

Pointers and arrays

Pointers and arrays are closely related in C. For instance, pointers can be used to point to an element in an array. Consider following array and pointer declarations:

int arr[5] = {1, 2, 3, 4, 5};
int *arrPtr = NULL;

The name of an array evaluates to the starting address of the array. Therefore, an array name can be considered as a pointer to the first array element. To make sure the pointer arrPtr points to the first element of the array arr the following statement can be used:

arrPtr = arr;

or alternatively:

arrPtr = &arr[0];
array and pointer in C

The value of the array element arr[2] can be accessed with the pointer expression:

*(arrPtr + 2);

arrPtr contains the starting address of the array. Adding an offset of 2 to that starting address, brings us to the element with index 2 inside the array.
Alternatively, since the array name and the pointer both contain the same memory address, we can use the array notation in combination with the pointer name.

arrPtr[2];

Or, we could move the pointer such that it points directly to the array element with index 2:

arrPtr += 2;

Example

#include <stdio.h>

int main(void)
{
 int numbers[5], *point;

 point = numbers;
 *point = 10;

 point++;
 *point = 20;

 point = &numbers[2];
 *point = 30;

 point = numbers + 3;
 *point = 40;

 point = numbers;
 *(point+4) = 50;

 for(int n=0; n<5; n++)
 printf("%d\t",numbers[n]);

return 0;
}

Output:

10 20 30 40 50

Summary

summary pointers


Card image cap
Guillermo Garcia Thanks for reading! I am one of the editors of this site and I am committed to providing you with the best information possible by sharing my experience in embedded systems.


Comments... There are no comments.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Publicidad