UNIT- 8
Pointer
The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
- Int n = 10;
- Int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
- Int *a;//pointer to int
- Char *c;//pointer to char
Pointer Example
An example of using pointers to print the address and value is given below.
As you can see in the above figure, 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.
Let's see the pointer example as explained for the above figure.
- #include<stdio.h>
- Int main(){
- Int number=50;
- Int *p;
- p=&number;//stores the address of number variable
- Printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.
- Printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
- Return 0;
- }
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Pointer to array
- Int arr[10];
- Int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.
Pointer to a function
- Void show (int);
- Void(*p)(int) = &display; // Pointer p is pointing to the address of a function
Pointer to structure
- Struct st {
- Int i;
- Float f;
- }ref;
- Struct st *p = &ref;
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.
Address Of (&) Operator
The address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable.
- #include<stdio.h>
- Int main(){
- Int number=50;
- Printf("value of number is %d, address of number is %u",number,&number);
- Return 0;
- }
Output
Value of number is 50, address of number is fff4
NULL Pointer
A pointer that is not assigned any value but NULL is known as the 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 provide a better approach.
Int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).
Pointer Program to swap two numbers without using the 3rd variable.
- #include<stdio.h>
- Int main(){
- Int a=10,b=20,*p1=&a,*p2=&b;
- Printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
- *p1=*p1+*p2;
- *p2=*p1-*p2;
- *p1=*p1-*p2;
- Printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
- Return 0;
- }
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Reading complex pointers
There are several things which must be taken into the consideration while reading the complex pointers in C. Lets see the precedence and associativity of the operators which are used regarding pointers.
Operator | Precedence | Associativity |
(), [] | 1 | Left to right |
*, identifier | 2 | Right to left |
Data type | 3 | - |
Here,we must notice that,
- (): This operator is a bracket operator used to declare and define the function.
- []: This operator is an array subscript operator
- * : This operator is a pointer operator.
- Identifier: It is the name of the pointer. The priority will always be assigned to this.
- Data type: Data type is the type of the variable to which the pointer is intended to point. It also includes the modifier like signed int, long, etc).
How to read the pointer: int (*p)[10].
To read the pointer, we must see that () and [] have the equal precedence. Therefore, their associativity must be considered here. The associativity is left to right, so the priority goes to ().
Inside the bracket (), pointer operator * and pointer name (identifier) p have the same precedence. Therefore, their associativity must be considered here which is right to left, so the priority goes to p, and the second priority goes to *.
Assign the 3rd priority to [] since the data type has the last precedence. Therefore the pointer will look like following.
- Char -> 4
- * -> 2
- p -> 1
- [10] -> 3
The pointer will be read as p is a pointer to an array of integers of size 10.
Example
How to read the following pointer?
- Int (*p)(int (*)[2], int (*)void))
Explanation
This pointer will be read as p is a pointer to such function which accepts the first parameter as the pointer to a one-dimensional array of integers of size two and the second parameter as the pointer to a function which parameter is void and return type is the integer.
Self-Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential in nature.
Example:
Structnode { Intdata1; Chardata2; Structnode* link; };
Intmain() { Structnode ob; Return0; } |
In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.
Types of Self Referential Structures
- Self-Referential Structure with Single Link
- Self-Referential Structure with Multiple Links
Self-Referential Structure with Single Link: These structures can have only one self-pointer as their member. The following example will show us how to connect the objects of a self-referential structure with the single link and access the corresponding data members. The connection formed is shown in the following figure.
#include <stdio.h>
Structnode { Intdata1; Chardata2; Structnode* link; };
Intmain() { Structnode ob1; // Node1
// Initialization Ob1.link = NULL; Ob1.data1 = 10; Ob1.data2 = 20;
Structnode ob2; // Node2
// Initialization Ob2.link = NULL; Ob2.data1 = 30; Ob2.data2 = 40;
// Linking ob1 and ob2 Ob1.link = &ob2;
// Accessing data members of ob2 using ob1 Printf("%d", ob1.link->data1); Printf("\n%d", ob1.link->data2); Return0; } |
Output:
30
40
Self-Referential Structure with Multiple Links: Self-referential structures with multiple links can have more than one self-pointers. Many complicated data structures can be easily constructed using these structures. Such structures can easily connect to more than one nodes at a time. The following example shows one such structure with more than one links.
The connections made in the above example can be understood using the following figure.
#include <stdio.h>
Structnode { Intdata; Structnode* prev_link; Structnode* next_link; };
Intmain() { Structnode ob1; // Node1
// Initialization Ob1.prev_link = NULL; Ob1.next_link = NULL; Ob1.data = 10;
Structnode ob2; // Node2
// Initialization Ob2.prev_link = NULL; Ob2.next_link = NULL; Ob2.data = 20;
Structnode ob3; // Node3
// Initialization Ob3.prev_link = NULL; Ob3.next_link = NULL; Ob3.data = 30;
// Forward links Ob1.next_link = &ob2; Ob2.next_link = &ob3;
// Backward links Ob2.prev_link = &ob1; Ob3.prev_link = &ob2;
// Accessing data of ob1, ob2 and ob3 by ob1 Printf("%d\t", ob1.data); Printf("%d\t", ob1.next_link->data); Printf("%d\n", ob1.next_link->next_link->data);
// Accessing data of ob1, ob2 and ob3 by ob2 Printf("%d\t", ob2.prev_link->data); Printf("%d\t", ob2.data); Printf("%d\n", ob2.next_link->data);
// Accessing data of ob1, ob2 and ob3 by ob3 Printf("%d\t", ob3.prev_link->prev_link->data); Printf("%d\t", ob3.prev_link->data); Printf("%d", ob3.data); Return0; } |
Output:
10 20 30
10 20 30
10 20 30
In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of the self-referential structure ‘node’. And they are connected using their links in such a way that any of them can easily access each other’s data. This is the beauty of the self-referential structures. The connections can be manipulated according to the requirements of the programmer.
Applications:
Self-referential structures are very useful in creation of other complex data structures like:
- Linked Lists
- Stacks
- Queues
- Trees
- Graphsetc
A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.
Implementation in C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
Struct node {
Int data;
Int key;
Struct node *next;
};
Struct node *head = NULL;
Struct node *current = NULL;
//display the list
VoidprintList(){
Struct node *ptr= head;
Printf("\n[ ");
//start from the beginning
While(ptr!= NULL){
Printf("(%d,%d) ",ptr->key,ptr->data);
Ptr=ptr->next;
}
Printf(" ]");
}
//insert link at the first location
VoidinsertFirst(int key,int data){
//create a link
Struct node *link =(struct node*)malloc(sizeof(struct node));
Link->key = key;
Link->data = data;
//point it to old first node
Link->next= head;
//point first to new first node
Head= link;
}
//delete first item
Struct node*deleteFirst(){
//save reference to first link
Struct node *tempLink= head;
//mark next to first link as first
Head= head->next;
//return the deleted link
ReturntempLink;
}
//is list empty
BoolisEmpty(){
Return head == NULL;
}
Int length(){
Int length =0;
Struct node *current;
For(current = head; current != NULL; current = current->next){
Length++;
}
Return length;
}
//find a link with given key
Struct node* find(int key){
//start from the first link
Struct node* current = head;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//go to next link
Current= current->next;
}
}
//if data found, return the current Link
Return current;
}
//delete a link with given key
Struct node*delete(int key){
//start from the first link
Struct node* current = head;
Struct node* previous = NULL;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//store reference to current link
Previous= current;
//move to next link
Current= current->next;
}
}
//found a match, update the link
If(current == head){
//change first to point to next link
Head= head->next;
}else{
//bypass the current link
Previous->next= current->next;
}
Return current;
}
Void sort(){
Inti, j, k,tempKey,tempData;
Struct node *current;
Struct node *next;
Int size = length();
k =size ;
For(i=0;i< size -1;i++, k--){
Current= head;
Next= head->next;
For( j =1; j < k ;j++){
If( current->data >next->data ){
TempData= current->data;
Current->data =next->data;
Next->data =tempData;
TempKey= current->key;
Current->key =next->key;
Next->key =tempKey;
}
Current= current->next;
Next=next->next;
}
}
}
Void reverse(struct node**head_ref){
Struct node*prev= NULL;
Struct node* current =*head_ref;
Struct node*next;
While(current != NULL){
Next= current->next;
Current->next=prev;
Prev= current;
Current=next;
}
*head_ref=prev;
}
Void main(){
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("Original List: ");
//print list
PrintList();
While(!isEmpty()){
Struct node *temp =deleteFirst();
Printf("\nDeleted value:");
Printf("(%d,%d) ",temp->key,temp->data);
}
Printf("\nList after deleting all items: ");
PrintList();
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("\nRestored List: ");
PrintList();
Printf("\n");
Struct node *foundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Delete(4);
Printf("List after deleting an item: ");
PrintList();
Printf("\n");
FoundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Printf("\n");
Sort();
Printf("List after sorting the data: ");
PrintList();
Reverse(&head);
Printf("\nList after reversing the data: ");
PrintList();
}
If we compile and run the above program, it will produce the following result −
Output
Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[ ]
Restored List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Element found: (4,1)
List after deleting an item:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Element not found.
List after sorting the data:
[ (1,10) (2,20) (3,30) (5,40) (6,56) ]
List after reversing the data:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Text Books
(i) Byron Gottfried, Schaum's Outline of Programming with C, McGraw-Hill
(ii) E. Balaguruswamy, Programming in ANSI C, Tata McGraw-Hill
Reference Books
(i) Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice Hall of India.