Select Page

Declaring pointers in C and working with them

by 02.03.2023C pointers

In the following example of code you can see the declaration of an integer (num) and of a pointer to an integer (ptr). The use of white space or tab around the asterisk is not relevant and is a matter of coder preference.

int	number; //integer declared
int	*ptr; //pointer to an integer declared

The asterisk is necessary as it declares the variable as a pointer.

For now both variables have not been initialized and contain garbage. If we speak about garbage it means that memory allocation can contain any value. Until the pointer is not initialized, it may not work properly.

How do we read a declaration of a pointer?

In the following example you can see how we can read the declaration of a pointer.

int	*ptr; //ptr is a variable
int	*ptr; //ptr is a pointer variable
int	*ptr; //ptr is a pointer variable to an integer

Let’s make it clear again. Pointer is a variable! Pointer stores an address in memory! In this address other variable can be stored!

Good practice to initialize a pointer as soon as possible

It is strongly recommended to initialize a pointer asap after its declaration as you can see in the example below:

	int	number;
	int	*ptr;
	
	number = 100;
	ptr = &number;

Printing the address of a pointer

To understand how pointers work, let’s try to print their address. For this we will use the function printf which is declared in the header file stdio.h. This header contains the declarations of all the basic functions for input and output.

#include <stdio.h>

int main(void)
{
	int	number;
	int	*ptr;
	
	number = 100;
	ptr = &number;
	printf("%p\n", ptr);
	return (0);
}

I saved the code above in the file main.c. Afterwards as you can see below I compiled the file using the command line “gcc main.c” in the Terminal. As the result the file “a.out” was created. With the command line “./a.out” I executed it and the address of pointer ptr was printed.

Terminal$ gcc main.c
Terminal$ ./a.out
0x7ffdf0d9f81c
Terminal$ ./a.out
0x7fff155888ec

As you can see above when you execute it another time, the address of pointer changed. Remember it. 🙂 Good news is that we can always access the address with the pointer.

Displaying Pointer values

#include <stdio.h>

int main(void)
{
	int	number;
	int	*ptr;
	
	printf("Address of ptr: %p, Value of number %i\n", ptr, number);
	number = 100;
	printf("Address of ptr: %p, Value of number %i\n", ptr, number);
	ptr = &number;
	printf("Address of ptr: %p, Value of number %i\n", ptr, number);
	printf("Address of ptr: %i, Value of number %i\n", ptr, number);

	return (0);
}

Terminal$ gcc main.c
Terminal$ ./a.out
Address of ptr: 0x1000, Value of number 0
Address of ptr: 0x1000, Value of number 100
Address of ptr: 0x7ffd6592cecc, Value of number 100
Address of ptr: 1704120012, Value of number 100

Conversion from hexadecimal number to decimal number

0x7ffdf0d9f81c
1613161216111610169168167166165164163162161160
1611 * 7166 * 0165 * 13163 * 15162 * 816 * 112 * 1

a = 10, b = 11, c = 12, d = 13, e = 14, f = 15

 (7 × 16¹¹) + (15 × 16¹⁰) + (15 × 16⁹) + (13 × 16⁸) + (15 × 16⁷) + (0 × 16⁶) + (13 × 16⁵) + (9 × 16⁴) + (15 × 16³) + (8 × 16²) + (1 × 16¹) + (12 × 16⁰) = (140728644270108)₁₀

Sources:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4
[2] PRINZ, Peter a Tony. C in a Nutshell: THE DEFINITIVE REFERENCE. 2nd Edition. Sebastopol: O’REILLY, 2016. ISBN 978-1-491-90475-6.