0. What are pointers?
Pointer is a data type that stores a memory address of a variable type. Pointers are widely used in C to handle data structures, dynamically allocate memory, and optimize performance in certain situations.
Every variable is associated with three things: datatype, name, value and address, that are stored in memory. We can easily access the address, using & before variable. Below is how we can see a address of a variable in C:
int variable = 2;
printf("The address of variable = %p.\n", &variable);
The function of pointer how i said above is store the address of a determinate variable, in code it works like that:
int *pointer = &variable;
or
int *pointer;
pointer = &variable;
When the pointer points nowhere, we give it the value null.
int *pointer2 = NULL;
To see if the pointer is really storing the address of the variable we do another printf to check, and now we just have to see if the addresses are the same.
printf("The address that the pointer stores: = %p.\n", pointer);
Now we can access the value of the "variable" through the pointer thereby using *: (*pointer)
printf("Value of variable: %d || Value of variable through pointer: %d\n", variable, *pointer);
Top comments (0)