Pointer 1
Pointer 1
Variable is a name given to a memory location. Each memory location has an address
which implies that each variable has an address of its own (which is an unsigned integer
value).
A pointer is a variable whose value is the address of another variable, i.e., direct
address of the memory location. Pointer concept is present in every programming
language because pointer is used to access the information from memory. Execution
speed is also faster in case of pointer. It is also useful for DMA (Dynamic memory
allocation).
The uses of pointers make the code more efficient. Some of the uses of pointers are-
Like any variable or constant, you must declare a pointer before using it to store any
variable address.
𝐝𝐚𝐭𝐚𝐭𝐲𝐩𝐞 ∗ 𝐩𝐨𝐢𝐧𝐭𝐞𝐫_𝐧𝐚𝐦𝐞;
It tells the compiler that the variable is a pointer and it can store the address of a
variable of the specified data type.
For example:
𝐢𝐧𝐭 ∗ 𝐩𝐭𝐫;
Here 𝐩𝐭𝐫 is a pointer that can hold the address of any 𝐢𝐧𝐭 variable.
𝐟𝐥𝐨𝐚𝐭 ∗ 𝐩𝟐;
Here 𝐩𝟐 is a pointer that can hold the address of any 𝐟𝐥𝐨𝐚𝐭 variable.
𝐜𝐩𝐭𝐫 is a pointer variable that points to variables of type 𝐜𝐡𝐚𝐫 and 𝐜𝟏 , 𝐜𝟐 are simple
variables . We can also write like this in one line.
Note: Content of a pointer is always a whole number irrespective of the data type.
Initialization:
Initialization Syntax:
𝐩𝐨𝐢𝐧𝐭𝐞𝐫_𝐧𝐚𝐦𝐞 = &𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒_𝑛𝑎𝑚𝑒;
For example:
int var=123;
int *ptr;
Types of pointer:
Untyped pointer: It can point to any data. It is otherwise known as generic pointer i.e
void *. It is discussed later.
There are a few important operations, which we will do with the help of pointers very
frequently.
#include <stdio.h>
int main () {
return 0;
When the above code is compiled and executed, it produces the following result −
1. & operator
2. * operator
1.& Operator:
We have already seen in the first example that we can display the address of a variable
using ampersand sign. I have used &var to access the address of variable var.
2.* Operator
The * Operator is also known as Value at address operator that returns the value of the
variable located at the address specified by its operand.
#include <stdio.h>
int main()
/* Pointer of integer type, this can hold the address of a integer type variable. */
int *p;
p= &var;
return 0;
Output:
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000. Assuming 32-bit integers, let us perform the following
arithmetic operation on the pointer:
ptr++
After the above operation, the ptr will point to the location 1004 because each time ptr is
incremented, it will point to the next integer location which is 4 bytes next to the current
location. This operation will move the pointer to the next memory location without
impacting the actual value at the memory location. If ptr points to a character whose
address is 1000, then the above operation will point to the location 1001 because the
next character will be available at 1001.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the variable
pointer can be incremented, unlike the array name which cannot be incremented
because it is a constant pointer. The following program increments the variable pointer
to access each succeeding element of the array:
#include <stdio.h>
int main () {
int i, *ptr;
ptr = var;
ptr++;
return 0;
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its value by
the number of bytes of its data type as shown below:
#include <stdio.h>
int main () {
int i, *ptr;
ptr = &var[MAX-1];
ptr--;
return 0;
When the above code is compiled and executed, it produces the following result:
Value of var[0] = 10
Pointer Comparisons
Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and
p2 point to variables that are related to each other, such as elements of the same array,
then p1 and p2 can be meaningfully compared.
Pointers can also be compared using relational (==, !=, <, >, <=, and >=) operators.
Two pointers are “equal” if they point to the same variable (i.e., the pointers have the
same value!)
A pointer p is “less than” some other pointer q if the address currently stored in p is
smaller than the address currently stored in q.
The following program modifies the previous example by incrementing the variable
pointer so long as the address to which it points is either less than or equal to the
address of the last element of the array, which is &var[MAX - 1]:
#include <stdio.h>
int main () {
int var[] = {10, 100, 200};
int i, *ptr;
ptr = var;
i = 0;
ptr++;
i++;
return 0;
When the above code is compiled and executed, it produces the following result:
Value of var[0] = 10
void main( )
p1=&a;
p2=&b;
printf(“Value=%d\n”, p1==p2);
printf(“Value=%d\n”, p1>p2);
printf(“Value=%d\n”, p1<p2);
p1=p2;
printf(“Value=%d\n”, p1==p2);
Output:
Value=0
Value=0
Value=1
Value=1
Example :
void main( ) {
p1=&a;
p2=&b;
printf("Sum=%d\n",*p1+*p2); /* Same as a + 𝑏 */
printf("Difference=%d\n",*p1-*p2); /* Same as a − b */
printf("Product=%d\n",(*p1)*(*p2)); /* Same as a ∗ b */
Output:
Sum=15
Difference=5
Product=50
Division=2
Example:
Let p1 and p2 be two pointers pointing to same type of variables, then the following:
If we perform arithmetic operations on pointer variables then we get the following result.
pa=1002 ptr=1010
Example:
float c , d;
XXX 1000
float *pc=&c, *pd=&d; c
XXX 1002
printf(“pc=%u\t”, pc); 1004
NULL Pointer
Dangling Pointer
Generic Pointer
Wild Pointer
Near Pointer
Far Pointer
Huge Pointer
1. Null Pointer
#include <stdio.h>
int main()
return 0;
Output :
void main()
.....
.....
char ch;
ptr = &ch;
3. Generic Pointers
When a variable is declared as being a pointer to type void, it is known as a generic
pointer. Since you cannot have a variable of type void, the pointer will not point to any
data and therefore cannot be dereferenced. It is still a pointer though, to use it you just
have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different
times.
Example of Generic Pointer
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
/* here the_data is apointer type casted from void to int data type.*/
the_data = &c;
return 0;
4. Wild Pointer
A Pointer in C that has not been initialized till its first use is known as the Wild pointer.
A wild pointer points to some random memory location.
Example :
int main()
{
int *ptr;
printf("%d", *ptr);
int val = 5;
printf("%d", *ptr);
5. Near Pointer
The pointer which can points only 64KB data segment or segment number 8 is
known as near pointer.
That is near pointer cannot access beyond the data segment like graphics video
memory, text video memory, etc. Size of near pointer is two byte. With the help of
keyword near, we can make any pointer as near pointer.
Example:
#include<stdio.h>
int main()
int x=25;
printf(“%d”,sizeof ptr);
return 0;
Output: 2
6. Far Pointer
The pointer which can point or access whole the residence memory of RAM, i.e.,
which can access all 16 segments is known as far pointer.
Size of the far pointer is 4 byte or 32 bit.
Example:
#include<stdio.h>
int main()
int x=10;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
Output : 4
7. Huge Pointer
The pointer which can point or access whole the residence memory of RAM i.e.
which can access all 16 segments is known as a huge pointer.
Size of the far pointer is 4 byte or 32 bit.
Example:
#include<stdio.h>
int main()
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
return 0;
Output: 4 4 1
p is the huge pointer, *p is the far pointer and **p is char type data variable.
Syntax:
Example:
int*pr2;
int **pr1;
Here pr1 is a double pointer. There must be two *’s in the declaration of double
pointer.
Let’s understand the concept of double pointer with the help of a diagram:
Variable Name
Variable Address
Here pr2 is a pointer holds number 123 and pointer pr1 holds the address of pr2.
Example:
void main( ){
p=&n;
ptr=&p;
printf(“n=%d\t”, n);
printf(“p=%u\n”, p);
printf(“ptr=%u\n”, ptr);
printf(“Value=%d\n”, **ptr);
Output:
n=5 p=1004
The precedence level of * operator and ++ / -- operators is same and their associativity
is from right to left.
i) x=*ptr++;
The expression *ptr++ is equivalent to *(ptr++) , since these operators associate from
right to left hence ++ operator will be applied to ptr not to * ptr. Here ++ operator is
postfix , so first value of ptr will be used in expression then it will be incremented. Hence
first the integer pointed to by ptr will be dereferenced and assigned to x then ptr will be
incremented. So the above expression is evaluated like
x= *ptr;
ptr=ptr+1;
*pi=&a;
int *p;
p=&a;
Find:
i) value of p ii) address of a iii) ++p iv) p++ v) --p vi) p-- vii) p
Advantages of Pointers: