Understand C pointers the easy way
One the most confusing part to understand in C is pointers and how to use them, In this article i will explain you what a pointer is, how…
One the most confusing part to understand in C is pointers and how to use them, In this article i will explain you what a pointer is, how we declare a pointer and how we use it, lets start!
Changing the value of a variable
Save the following file as example1.c , this is a very simple C program that declares an integer named number and assigns the value of 1 prints the variable and then changes the variable value to 2 and then prints the variable again
#include <stdio.h>
int main()
{
int number;
number = 1;
printf("%d\n",number);
number = 2;
printf("%d\n",number);
}We can compile and run our program to verify this
$ gcc -o example1 ./example1.c
./example1
1
2Very simple right? lets make the program a bit more complicate, in real life more likely you have to pass variables between functions! lets try it! save the following as example2.c
#include <stdio.h>
void modify_variable(int number);
int main()
{
int number = 1;
printf("Variable initial value in main function is: %d\n",number);
modify_variable(number = number);
printf("I am inside main function again: %d\n",number);
}
void modify_variable(int number)
{
number++;
printf("I am inside function, now variable has value: %d\n",number);
}This file is a bit complicate but dont worry everything will be clear, we start by creating our number variable in main function and assign the value of 1 then we print the variable, what we do next is we pass the variable to the modify_variable function which is very simple! it just increments the variable by one and then returns the program flow back to the main function which again prints the variable, what do you believe will print on screen? lets compile and run the program!
gcc -o example2 ./example2.c
./example2
Variable initial value in main function is: 1
I am inside function, now variable has value: 2
I am inside main function again: 1We can see that the variable increased by one inside the modify_variable function but when we returned back to the main function the variable was again 1! why this is happened??. The explaination is quite simple, when we pass a variable in a C function like this we dont pass the variable its self, we actually pass the value of the variable which is stored in a variable visible only to the function scope, when we return back to the main we can verify that the variable has not beed modified by the modify_variable function.
But how i can change the value of a variable from another function? here comes the concept of pointers.
What is a pointer
A pointer is a special kind of variable that stores the memory address of another variable, a pointer differs its declaration from normal variables using an asterisk in front of the variable name, also like normal variables pointers also need to have a data type, in this case our pointer is an integer pointer, you will understand later on in this article why a pointer needs to have a data type!
int *my_var;Save the following file as example3.c
#include <stdio.h>
int main()
{
int *ptr_a;
int a=1;
ptr_a = &a;
printf("%p\n",ptr_a);
printf("%d\n",*ptr_a);
}Compile and run the program
gcc -o example3 ./example3.c
./example3
0x16f26f4d4
1Not very impressive but many things happened on the background! lets examine the program line by line
This line creates a pointer variable of integer type!, as we said before asterisk before the variable name is required to declare a pointer.
int *ptr_a;Next
int a = 1;
ptr_a = &a;Here happens all the pointer magic! we created a simple integer variable named a with a value of 1 then, we passed to the pointer variable the memory address of variable a using the & operator!
ptr_a = &a;Now the pointer ptr_a holds a the memory address of variable a , using the "%p" formatter we can print the memory address in hex form!
printf("%p\n",ptr_a);
0x16f26f4d4To access the memory address value we need to use the dereference operator which is the same as the declaration operator, the asterisk!
printf("%d\n",*ptr_a);
1And here comes why pointers need data types! if the pointer was just a plain memory address we would not be sure how dereference the value of the memory address! will be treated as an integer? a double? a char? how the compiler would know how many bytes to read from the memory address?.
Lets do our final example on how to modify values by reference! save the following file as example4.c
#include <stdio.h>
void modify_value(int *number);
int main()
{
int a = 1;
printf("%p\n",&a);
modify_value(&a);
printf("%d\n",a);
}
void modify_value(int *number)
{
printf("%p\n",number);
*number = *number + 1;
}Compile and run
gcc -o example4 ./example4.c
./example4
0x16b6374dc
0x16b6374dc
2Lets explain what just happened!.
- Inside the
mainfunction we created an integer variable namedawith an initial value of1 - next we printed out just for reference its memory address (note that in your computer will print another memory address).
- Then we passed the memory address of the variable to function
modify_value, Note the syntax of the function, as input parameter accepts an integer pointer and not a simple integer! - Then inside the
modify_valuefunction we print the pointer to show you that its actually the address of theavariable. - Then by dereferencing using the asterisk operator we ask the compiler to get the value of this memory address and to add
1to this value. - Now the control has been returned to the
mainfunction and we can verify that the value ofavariable has been modified by themodify_valuefunction!
Conclusion
In this article we saw how we can use pointers easily to perform basic operations like modyfing variables from one function to another! of course there are more advanced pointers topics to cover but i think that starting with really simple examples is what you need most of the times to go on!