In this tutorial, we will learn the about the pointers in c, how do they help in programming and how does it function.
First of all before starting pointer one should have knowledge about how memory is organized in C.
Here, the above image shows the layout of computer memory simply. When a variable is defined, it is allocated a portion of memory. 6.5 is the variable and 12,004,234 is the address for where that value resides.
A pointers is a variable whose value is the address of another variable.
The size of a pointer type determines the maximum value it can represent.
For example, a 16-bit pointer can only handle addresses between 0 and 216 − 1 (i.e., 65535).
As 16-bit machines could only hold address of 65535 size 32-bit machines were developed. 32-bit machines was toenable greater memory addressing; a 32-bit pointer can address 0 to 232 − 1 (i.e., 4294967295) bytesof memory.
Example of Address in C
We have used address in C from the start of tutorial on C programming.
int num= 100;
printf("Enter any number : ");
printf("Value: %d\n", num);
printf("Address: %u", &num); //Notice, the ampersand(&) before num.
Here, scanf takes the entered number by user then stores it into the address of num.
Here, num is the name of the location where value 100 is stored. Where as &num gives the memory location of var.
Syntax of pointer
A pointer is declared using ( * ) symbol.
int i = 2;
int *j = &i;
int x = *j;
Here, pointer j is initialized with the address of i.x = *j in this expression, the x is assigned the value of i i.e 2.