Pointers in C Programming Language
Posted on December 9, 2017
In c the variables take up space a in memory, namely in RAM memory. We can say that ram memory is a huge vector of bytes where all variables are stored. Each variable is stored in a specific memory address. Where the positions inidices vary between 0 and the total bytes of the Ram memory. Pointers in c allow us manipulate directly space in memory. When we declare a variable in c, we are saying to the compiler to save a certain space in memory to store the content of that variable.
Declare Pointers in C
variable_type * variable_name;
- variable_type, we already have learn;
- float, int, char, double.
- * indicates that it is a pointer-type variable;
- variable_name, is the name of the pointer that follow the variable name declarations.
This is an example of how to declare a pointer in c:
-> int *p;
-> int* p;
-> int * p;
Using this three ways we are declaring pointers, regardless of whether it is attached to the variable name or variable type .
We have being using pointers since the beginning. When we call the function scanf and use & to store the content of the variable. We are saying to the compiler to save the variable content in the address of that variable.
When we use this we are passing by reference the address of variable pointer.
How to Assign an Address to a Pointer?
Use pointers in c is a very delicate process. In c it is possible to assign an address to a pointer bellow:
pointer = &number;
- We declare a variable number of type integer, and a pointer named pointer of type int;
- Then we are assigning the address to pointer variable using &.
- get the address of variable number using, &number.
If we do not want our pointer to return no value we assign it the NULL constant.
pointer = NULL;
Pointer of a Pointer
And if we want know the address of a pointer?
- This is a tricky, but simple process. Works the same way when we declare a pointer:
//declare an integer variable with value 7
int number = 7;
//we declare a variable of type pointer
//we declare a variable of type pointer of pointer using **
//we are giving the adress of variable number to the pointer 'pointer_number'
pointer_number = &number;
//we are giving the adress of pointer pointer_number to the pointer 'pointer_pointer_number'
pointer_pointer_number = &pointer_number;
Do you have any doubt about pointer? Please comment bellow!