Module - Iv: Pointers
Module - Iv: Pointers
MODULE – IV
Pointers
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.
Declaration and Initialization of Pointer:-
data_type *pointer_variable_name;
Ex:- int *p1; int *p2;
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. (& is called as ampersand, * is called as asterisk)
Pointer Example:
Let Ptr is a pointer variable that contains the address of another variable Q. then Ptr is
said to be pointing to Q. If the address of Q is 642230 then the value of Ptr would be 642230.
Using this address, we can manipulate the value of Q. It can be as shown below:
1
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example – 2:
// Swap Two Numbers using pointer
#include <stdio.h>
void main()
{
int a, b, *p, *q, temp;
p = &a;
q = &b;
printf("Enter two numbers \n");
scanf("%d%d", &a, &b);
printf("Before swapping: %d %d \n", a, b);
temp = *p;
*p = *q;
2
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
*q = temp;
printf("After Swapping %d %d \n", *p, *q);
}
Output:
Enter two numbers
10 20
Before swapping: 10 20
After Swapping 20 10
Pointer Arithmetic
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on
pointers These operations are:
1. Increment of a Pointer ( ++ )
2. Decrement of a Pointer ( -- )
3. Addition of integer to a pointer (+)
4. Subtraction of integer to a pointer (-)
5. Comparison of pointers ( > and <)
Increment of a Pointer:
It means increment a pointer by the pointer with start pointing to the immediate exist
locaton.
Example:
int n = 10;
int *ptr;
ptr = &n;
ptr++; // ptr = ptr + 1;
Pointer incremented by 1 and now start pointing to the immediate next location.
new – address = current – address + i * sizeof(data_type)
Decrement of a Pointer:
It means decrement a pointer by 1 the pointer will start pointing to the immediate
previous location.
Example:
int n = 10;
int * ptr;
ptr = &n;
ptr--; // ptr = ptr - 1;
Pointer decremented by 1 and now start pointing to the immediate previous location.
3
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Comparison of Pointers:
We can compare the two pointers by using the comparison operators ( > and <). It
returns true for the valid condition and returns false for the in-valid condition.
4
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example:
int *p1, *p2, x, y;
p1 = &x;
p2 = &y;
Note: p1 < p2 and p1 > p2 are valid operations.
//Pointer Arithmetic Example Program
#include <stdio.h>
void main()
{
int n1 = 10, n2 = 12;
int *ptr1, *ptr2;
ptr1 = &n1;
ptr2 = &n2;
printf("ptr1 = %d \n", ptr1);
ptr1++;
printf("Increment = %d \n", ptr1);
ptr1--;
printf("Decrement = %d \n", ptr1);
printf("Addition = %d \n", ptr1+3);
printf("Subtraction = %d \n", ptr1-3);
printf("ptr1 = %u - ptr2 = %u \n", ptr1, ptr2);
printf("Comparision = %u \n", ptr1 > ptr2);
}
Output:
ptr1 = 6422292
Increment = 6422296
Decrement = 6422292
Addition = 6422304
Subtraction = 6422280
ptr1 = 6422292 - ptr2 = 6422288
Comparision = 1
Array of Pointers
An array of pointers in C is used when we need to point to multiple variables or
memory locations of the same data type
Syntax:
data_type *name_of_array [array_size];
Example:1 To represent array of pointers
#include <stdio.h>
void main()
{
int arr[5] = {45, 65, 85, 35, 78};
int i;
5
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
int *ptr;
for (i = 0; i < 5; i++)
{
ptr = &arr[i];
printf("Adress of Array[%d] = %u \n", i, ptr);
ptr++;
}
}
Output:
Adress of Array[0] = 6422276
Adress of Array[1] = 6422280
Adress of Array[2] = 6422284
Adress of Array[3] = 6422288
Adress of Array[4] = 6422292
Output:
Enter 5 elements of the array : 10 20 30 40 50
Sum of array elements: 150
6
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
7
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Output:
Address of x = 6422300
Address of x = 6422300
Address of pointer a = 6422296
Address of pointer a = 6422296
Address of pointer b = 6422292
Need of Structure
Assume that we are going to develop a software for the college library to manage all
students information. Student information includes roll number, name, age, date of birth, and
address.
Example,
Structure
Structure in c is a user-defined data type that enables us to store the collection of
different data types. Each element of a structure is called a member. The, struct keyword is
used to define the structure.
8
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
9
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example Program:
#include <stdio.h>
struct employee
{
int id;
char name[50];
float sal;
};
void main()
{
struct employee e1,e2;
printf("Enter employee-1 Id, Name, Salary: ");
scanf("%d %s %f", &e1.id, &e1.name, &e1.sal);
printf("Enter employee-2 Id, Name, Salary: ");
scanf("%d %s %f", &e2.id, &e2.name, &e2.sal);
printf("Id \t Name \t Salary \n");
printf("%d \t %s \t %.2f \n", e1.id, e1.name, e1.sal);
printf("%d \t %s \t %.2f \n", e2.id, e2.name, e2.sal);
}
Output:
Enter employee-1 Id, Name, Salary: 5502 Ravi 54634
Enter employee-2 Id, Name, Salary: 5512 Giri 54323
Id Name Salary
5502 Ravi 54634.00
5512 Giri 54323.00
Let's see another way to declare variable at the time of defining structure.
struct employee
{
int id;
char name[50];
float salary;
} e1, e2;
10
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example Program:
#include <stdio.h>
struct employee
{
int id;
char name[50];
float sal;
} e1, e2;
void main()
{
//struct employee e1,e2;
printf("Enter employee-1 Id, Name, Salary: ");
scanf("%d %s %f", &e1.id, &e1.name, &e1.sal);
printf("Enter employee-2 Id, Name, Salary: ");
scanf("%d %s %f", &e2.id, &e2.name, &e2.sal);
printf("Id \t Name \t Salary \n");
printf("%d \t %s \t %.2f \n", e1.id, e1.name, e1.sal);
printf("%d \t %s \t %.2f \n", e2.id, e2.name, e2.sal);
}
Output:
Enter employee-1 Id, Name, Salary: 5502 Ravi 54634
Enter employee-2 Id, Name, Salary: 5512 Giri 54323
Id Name Salary
5502 Ravi 54634.00
5512 Giri 54323.00
int main()
{
struct Employee emp;
emp.id = 101;
strcpy(emp.name, "Hari");
11
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example 2:
#include <stdio.h>
struct Employee
{
int id;
char name[20];
};
int main()
{
struct Employee emp[2] = {{101, "Hari"}, {102, "Ravi"}};
printf("ID: %d, Name: %s\n", emp[0].id, emp[0].name);
printf("ID: %d, Name: %s\n", emp[1].id, emp[1].name);
return 0;
}
Output:
ID: 101, Name: Hari
ID: 102, Name: Ravi
int main()
{
struct Employee emp = {101, "Hari"};
struct Employee *ptr = &emp;
printf("ID: %d, Name: %s\n", ptr->id, ptr->name);
return 0;
}
Output:
ID: 101, Name: Hari
Example 2:
#include <stdio.h>
struct Employee {
12
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
int id;
char name[20];
};
int main() {
struct Employee emp[2] = {{101, "Hari"}, {102, "Ravi"}};
struct Employee *ptr[2] = {&emp[0], &emp[1]};
printf("ID: %d, Name: %s\n", ptr[0]->id, ptr[0]->name);
printf("ID: %d, Name: %s\n", ptr[1]->id, ptr[1]->name);
return 0;
}
Output:
ID: 101, Name: Hari
ID: 102, Name: Ravi
Nesting of Structures
Nesting structures involves defining one structure inside another. This allows for
complex data organization and representation.
Define the inner structure (Address) before the outer structure (Employee).
Declare an instance of the inner structure (address) within the outer structure.
Access nested members using the dot operator (.).
Example Program:
#include <stdio.h>
// Inner structure
struct Address
{
char street[20];
char city[20];
int zip;
};
13
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
int main()
{
// Initialize employee structure
struct Employee emp = { 101, "Hari", {"123 Main St", "Nellore",
524001}};
Output:
ID: 101
Name: Hari
Address: 123 Main St, Nellore 524001
Array of Structures
An array of structures can be defined as the collection of multiple structures variables
where each variable contains information about different entities. The array of structures is
used to store information about multiple entities of different data types. The array of
structures is also known as the collection of structures.
14
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Example Program:
#include <stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students \n");
for (i = 0; i < 5; i++)
{
printf("Enter Rollno:");
scanf("%d", &st[i].rollno);
printf("Enter Name:");
scanf("%s", &st[i].name);
}
printf("\nStudent Information List:");
printf("\nRollno \t Name \t");
for (i = 0; i < 5; i++)
{
printf("\n%d \t %s", st[i].rollno, st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:501
Enter Name:Hari
Enter Rollno:502
Enter Name:Giri
Enter Rollno:503
Enter Name:Ravi
15
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Enter Rollno:504
Enter Name:Sita
Enter Rollno:505
Enter Name:Gita
Self-Referential Structure
A self-referential structure is a structure that contains a pointer to itself, either directly
or indirectly. This allows for dynamic data structures such as linked lists, trees, and graphs.
Syntax:
The above syntax, data1 and data2 are stores the node's value. The link is a pointer to
the next node in the linked list.
Example:
struct node
{
int data1;
char data2;
struct node *link;
};
int main()
{
struct node ob;
return 0;
}
16
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Unions
Unions are conceptually similar to structures. The syntax of union is also similar to
that of structure. The only difference is in terms of storage. In structure each member has its
own storage location, whereas all members of union use a single shared memory location
which is equal to the size of its largest data member.
Memory Efficiency
Flexible Data Storage
Compiler Design
Embedded Systems
Network Protocols
Data Compression
17
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
Declaration:
union union_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Initializatioin:
union union_name union_variable = {value1, value2, ……..};
Example:
union employee
{
int id;
char name[50];
float salary;
};
Example Program:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main()
{
union Data data;
data.i = 10; // Store integer
data.f = 20.5; // Store float
strcpy(data.str, "Hello"); // Store string
printf("Integer: %d\n", data.i);
printf("Float: %f\n", data.f);
18
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur
INTRODUCTION TO PROGRAMMING I B.TECH I SEM (R23)
void main()
{
printf("Size of union = %u bytes \n", sizeof(u));
printf("Size of structure = %u bytes \n", sizeof(s));
}
Output:
Size of union = 32 bytes
Size of structure = 40 bytes
19
P. Srihari, Assistant Professor, Narayana Engineering College, Gudur