Pointer in C

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

1

POINTERS:
 A pointer in C language is a variable that stores/points the address of another variable. A
Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable
might be belonging to any of the data type such as int, float, char, double, short etc.
 Pointer Syntax : data_type *var_name;
 Example : int *p;
char *p;
Where, * is used to denote that “p” is pointer variable.
Reference operator (&) and Dereference operator (*)
 & is called reference operator. It gives the address of a variable.
 dereference operator (*):gets the value from the address
Demonstration of & and * Operator
#include <stdio.h>
int main()
{
int *p;
int var = 10;
p= &var;
printf("Value of variable var is: %d", var);
printf("\n Value of variable var is: %d", *p);
printf("\n Address of variable var is: %p", &var);
printf("\n Address of variable var is: %p", p);
printf("\n Address of pointer p is: %p", &p);
return 0;
}
Output:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0061FF18
Address of variable var is: 0061FF18
Address of pointer p is: 0061FF1C
An example of using pointers printing the address and value is given below.

Here, pointer variable stores the address of number variable i.e. fff4. The value of number
variable is 50. But the address of pointer variable p is aaa3. By the help of * (indirection
operator), we can print the value of pointer variable p.
2

#include <stdio.h>
#include <conio.h>
void main()
{
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

Benefits of using Pointers in C


1. Pointers reduce the length and complexity of a program.
2. They increase execution speed.
3. A pointer enables us to access a variable that is defined outside the function.
4. Pointers are more efficient in handling the data tables.
5. The use of a pointer array of character strings results in saving of data storage space in
memory.

NULL Pointer
A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have
any address to be specified in the pointer at the time of declaration, you can assign NULL value.
It will a better approach.
int *p=NULL;

C POINTER TO POINTER
In C pointer to pointer concept, a pointer refers to the address of another pointer.
In c language, a pointer can point to the address of another pointer which points to the address of
a value. Let's see the syntax of pointer to pointer.
int **p2;
Let's see an example where one pointer points to the address of another pointer.
3

In the above figure, p2 contains the address of p (fff2) and p contains the address of number
variable (fff4).
#include <stdio.h>
#include <conio.h>
void main()
{
int number=50;
int *p; //pointer to int
int **p2; //pointer to pointer
p=&number; //stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",**p);
}

Output
Address of number variable is fff4
Address of p variable is fff4
Value of *p variable is 50
Address of p2 variable is fff2
Value of **p variable is 50

POINTER ARITHMETIC IN C
In C pointer holds address of a value, so there can be arithmetic operations on the pointer
variable. Following arithmetic operations are possible on pointer in C language:
 Increment
 Decrement
 Addition
 Subtraction
 Comparison

1. Incrementing Pointer in C
Incrementing a pointer is used in array because it is contiguous memory location.
Moreover, we know the value of next location. Increment operation depends on the data type of
the pointer variable. The formula of incrementing pointer is given below:
new_address= current_address + i * size_of(data type)
2. Decrementing Pointer in C
Like increment, we can decrement a pointer variable. The formula of decrementing pointer is
given below:
1. new_address= current_address - i * size_of(data type)
4

Let's see the example of incrementing and decrementing pointer variable on 32 bit OS.
#include <stdio.h>
void main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p);
p=p-1;
printf("After decrement: Address of p variable is %u \n",p);
}
Output
Address of p variable is 3214864300
After increment: Address of p variable is 3214864302
After decrement: Address of p variable is 3214864300

3. C Pointer Addition
We can add a value to the pointer variable. The formula of adding value to pointer is given
below:
new_address= current_address + (number * size_of(data type))
4. C Pointer Subtraction
Like pointer addition, we can subtract a value from the pointer variable. The formula of
subtracting value from pointer variable is given below:
new_address= current_address - (number * size_of(data type))
#include <stdio.h>
void main(){
int number=50;
int *p; //pointer to int
p=&number; //stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n",p);
}
Output
Address of p variable is 3214864300
After adding 3: Address of p variable is 3214864306
After subtracting 3: Address of p variable is 3214864300

Here, we were using 32 bit OS, it were incrementing to 6 only i.e. 2*3=6. As integer value
occupies 2 byte memory in 32 bit OS.
5

5. C Pointer Comparison
 Used to compare two pointers.
#include<stdio.h>
int main()
{
int *ptr1,*ptr2,a=5,b=5;
ptr1 =&a;
ptr2 =&a;
if(ptr1 == ptr2)
printf("Ptr1 and Ptr2 are equal");
else
printf("Ptr1 and Ptr2 are not equal");
return(0);
}
6

ARRAY OF POINTERS
Following is the declaration of an array of pointers to an integer −
int *ptr[MAX];
It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer to
an int value. The following example uses three integers, which are stored in an array of pointers,
as follows −

#include <stdio.h>
const int MAX = 3;
void main () {
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
}
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

You can also use an array of pointers to character to store a list of strings as follows −
#include <stdio.h>
const int MAX = 4;
void main () {
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};

int i = 0;
for ( i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i] );
}
}
When the above code is compiled and executed, it produces the following result −
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
7

You might also like