- Pointer is the most important feature of C programming language, if you want to use power of C programming language then you should must know pointer concept.
- The concept of pointer is very simple, but in starting you may feel some confusion but once you get to know the concept of pointer, you will love to use it.
- Concept of pointer totally depends on memory address and value stored on that address, so let’s first know what is memory address and how a value get stored there.
A computer memory (RAM) divided in several small – small parts, an individual part is called as cell and every data item stored in these cells. Since computer memory is sequential collection of cells and data value stored at one or more contiguous memory cells.
Each cell in memory has it’s unique identity which is called it’s address and it is an unique number, just like a house number in building, means each cell is marked with an unique number which is called cell address.
The number of cell required to store a data value is depends on types of data for example char type data requires 1 byte ( 8 bits ) to store a single character.
Variable name, value and address
As you know to declare a variable requires a variable name, data type and value of variable:
Let’s see how this variable P get stored in memory with the help of an image.
Address → &
In above example, the name of variable is P, value at address is 30, and address is 2024. To access address of variable we use address operator ( & ). See below lines.
printf("%d \n", P);
printf("%d", &P);
Output
Where:
- P → Value of P
- &P → Address of P
Variable address is not fixed..?
Since memory is divided in many cells and each cell has unique address( number). When a program runs, the variables used in program are assigned to cells to get address where variable’s value can be stored. Now each time when we run program the variable may assigned to different cells, which address is different from previous cell, so address of a variable is not fixed it may change each time.
Pointer in C
Pointer is a variable who store the address of another variable OR A pointer points to a memory location OR A pointer is used to access the information of a particular memory location.
A pointer is just like other variable, there is only one difference that a pointer only can store variable’s address which is a number. Let’s see the declaration of a pointer.
Pointer Declaration
Example
int *ptrP;
Here
- ptrP → is a pointer variable, we can name it any thing except c keywords.
- * → Asterisk(*), indicates that this variable is a pointer.
- data_type → Data type of pointer's object.
A pointer can be declared in three ways but above given technique is used most frequently. Other two techniques are given below.
- int* ptrP;
- int * ptrP;
- There is only difference of location of * symbol, it can be anywhere in between data type and pointer variable name.
Rules of Pointer's Naming
- Pointer name should not a C Keywords.
- Pointer name should follow all rule of variable naming convention.
Pointer Initialization
A pointer only can be initialized by an address of a variable.
Data type of pointer and data type of that variable must be same whose address is being initialized to the pointer.
int P=30;
int *ptrP; // pointer declaration
ptrP = &P; // pointer initialization
-------- OR ---------
int x, *p=&x;
Wrong Initialization
int P=30;
int *ptrP;
ptrP = &P; // Wrong, data type of P and ptrP are different
-------- OR ---------
int *p=&x, x; // Wrong, variable must be declare first
Don’t be Confuse
- Data type of a pointer object tells the data type of the value at that address which is initialized by a pointer.
- In other words pointer’s data type tells about the variable data type which address is stored in that pointer.
- A pointer value is always an unsigned number.
Accessing Pointer value
Here are two things which are very important to understand, one you get to know after that you will love to work with pointer. Let’s see.
1. Pointer value
It is address of variable which address stored in pointer, it may change each time when we run program, simply it is accessed by pointer_name.
int P=30;
int *ptrP;
ptrP = &P;
printf("pointer value = %d", ptrP);
Output
Let's assume compiler assign the address of variable P at 2024, then output
2. Pointer value points to
It is value at address which is stored in pointer, simply it is the value of variable which address stored in pointer. It is accessed by *pointer_name
- Always remember this “ Pointer points value at address”
- It's value is always same, when each time we run program.
int P=30;
int *ptrP;
ptrP = &P;
printf("Pointer points to = %d", *ptrP); // see * before variable name
Output
See below image for clear understanding, Please remember address may change each time when you run program each time.
Example
See below program to understand how you can use pointer in programming, in different ways:
Output
Example
What will be the output of program?
Output