Professional Documents
Culture Documents
Pointers Types o Pointrs by Abhilash
Pointers Types o Pointrs by Abhilash
Pointers Types o Pointrs by Abhilash
If you have a variable var in your program, &var will give you its address in the memory.
We have used address numerous times while using the scanf() function.
scanf("%d", &var);
#include <stdio.h>
int main()
int var = 5;
return 0;
var: 5
C Pointers
Pointers (pointer variables) are special variables that are used to store addresses rather than values.
int* p;
Here, we have declared a pointer p of int type. You can also declare pointers in these ways.
int *p1;
int * p2;
Let's take another example of declaring pointers.
int* pc, c;
c = 5;
pc = &c;
Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.
To get the value of the thing pointed by the pointers, we use the * operator. For example:
int* pc, c;
c = 5;
pc = &c;
a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.
c) To check for null pointer before accessing any pointer variable. So that, we can perform error handling
in pointer related code e.g. dereference pointer variable only if it’s not NULL.
int* pInt = NULL;
C programming tasks are performed more easily with pointers, and other tasks, such as dynamic
memory allocation, cannot be performed without using pointers. So it becomes necessary to learn
pointers to become a perfect C programmer.
Every variable is a memory location and every memory location has its address defined which can be
accessed using ampersand (&) operator, which denotes an address in memory.
EXAMPLE
The data type of pointer and the variable must match, an int pointer can hold the address of int variable,
similarly a pointer declared with float data type can hold the address of a float variable. In the example
below, the pointer and the variable both are of int type.
#include <stdio.h>
int main()
//Variable declaration
//Pointer declaration
int *p;
p=#
return 0;
Output:
Address of variable num is: 0x7fff5694dc58