Unit 6
Unit 6
Basics of pointers
● Pointers are used for dynamic memory allocation.
● every variable is a memory location and every memory location has its address defined
which can be accessed using ampersand (&) operator, which denotes an address in
memory.
#include <stdio.h>
int main ()
{
int var1;
char var2[10];
return 0;
}
Output:
● A pointer is a variable whose value is the address of another variable, i.e., direct address
of the memory location. Like any variable or constant, you must declare a pointer before
using it to store any variable address.
● 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.
int n = 10;
int* p = &n;
Declaration of a pointer
● The pointer in c language can be declared using * (asterisk symbol). It is also known as
an indirection pointer used to dereference a pointer.
● int *a;//pointer to int
● char *c;//pointer to char
#include<stdio.h>
2
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
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.
Output:
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 a NULL value. It will provide a better approach.
● int *p=NULL;
● In 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:
3
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
Advantages of Pointer
● Pointer reduces the code and improves the performance, it is used for retrieving strings,
trees, etc. and used with arrays, structures, and functions.
● We can return multiple values from a function using the pointer.
● It makes you able to access any memory location in the computer's memory.
Pointer to Pointer
● We can also define a pointer to store the address of another pointer. Such a pointer is
known as a double pointer (pointer to pointer).
● The first pointer is used to store the address of a variable whereas the second pointer is
used to store the address of the first pointer.
Syntax:
● int **p; // pointer to a pointer which is pointing to an integer.
4
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
Pointer Array
It generally stores the address of another Array usually stores the value of the
variable of the same data type as the pointer variable of the same datatype.
variable's data type.
Pointers are specially designed to store the A normal array stores values of variables,
address of variables. and a pointer array stores the address of
variables.
Usually, arrays can store the number of A pointer variable can store the address
elements the same size as the size of the of only one variable at a time.
array variable.
when the arrays are implemented, the fixed where the pointers are implemented, the
size of the memory block is allocated. memory is dynamically allocated.
pointer to array
● Pointers to an array point to the address of the memory block of an array variable.
● The following is the syntax of array pointers.
● datatype *variable_name[size];
● Here,
● datatype − The datatype of variable like int, char, float etc.
● variable_name − This is the name of variable given by user.
● size − The size of array variable.
C Program
#include<stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}
Output:
5
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
● A pointer variable in C stores the address of another variable. The address is always an
integer. So, can we perform arithmetic operations such as addition and subtraction on the
pointers? In this chapter, we will explain which arithmetic operators use pointers in C as
operands, and which operations are not defined to be performed with pointers.
● C pointers arithmetic operations are different from the general arithmetic operations.
The following are some of the important pointer arithmetic operations in C:
● Increment and Decrement of a Pointer
● Addition and Subtraction of Integer to Pointer
● Subtraction of Pointers
● Comparison of Pointers
x++; // x becomes 11
● What happens if we declare "y" as pointer to "x" and increment "y" by 1 (with "y++")?
Assume that the address of "y" itself is 2000.
int x = 10; // created at address 1000
int main(){
int x = 10;
int *y = &x;
6
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
y++;
int main(){
double x = 10;
double *y = &x;
y--;
● When an array is declared, the elements are stored in adjacent memory locations. In case
of "int" array, each array subscript is placed apart by 4 bytes, as the following figure
shows −
● Hence, if a variable stores the address of 0th element of the array, then the "increment"
takes it to the 1st element.
int main(){
int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int len = sizeof(a)/sizeof(int);
int *x = a;
7
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
int i = 0;
return 0;
}
Output
Address of subscript 0 = 6421984 Value = 10
Address of subscript 1 = 6421988 Value = 20
Address of subscript 2 = 6421992 Value = 30
Address of subscript 3 = 6421996 Value = 40
Address of subscript 4 = 6422000 Value = 50
Address of subscript 5 = 6422004 Value = 60
Address of subscript 6 = 6422008 Value = 70
Address of subscript 7 = 6422012 Value = 80
Address of subscript 8 = 6422016 Value = 90
Address of subscript 9 = 6422020 Value = 100
ptr = 123400
ptr = ptr + 1
ptr = ptr + sizeof(int)*1
ptr = 123400 + 4
ptr = 123404
In the following example, we are declaring an array and pointer to an array. Initializing the
pointer with the first element of the array and then adding an integer value (2) to the pointer to
get the third element of the array.
#include <stdio.h>
8
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = int_arr;
// Adding 2 in ptrArr
ptrArr = ptrArr + 2;
return 0;
}
Output
Value at ptrArr: 12
Value at ptrArr after adding 2: 45
In the following example, we are declaring an array and pointer to an array. Initializing the
pointer with the last element of the array and then subtracting an integer value (2) from the
pointer to get the third element of the array.
#include <stdio.h>
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = &int_arr[4]; // points to last element
// Subtracting 2 in ptrArr
ptrArr = ptrArr - 2;
return 0;
}
Output
Value at ptrArr: 89
Value at ptrArr after adding 2: 45
Subtraction of Pointers
● We are familiar with the "+" and "−" operators when they are used with regular numeric
operands. However, when you use these operators with pointers, they behave in a little
different way.
9
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
● Since pointers are fairly large integers (especially in modern 64-bit systems), addition of
two pointers is meaningless. When we add a 1 to a pointer, it points to the next location
where an integer may be stored. Obviously, when we add a pointer (itself a large integer),
the location it points may not be in the memory layout.
● However, subtraction of two pointers is realistic. It returns the number of data types that
can fit in the two pointers.
Let us take the array in the previous example and perform the subtraction of pointers of a[0] and
a[9]
#include <stdio.h>
int main(){
int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int *x = &a[0]; // zeroth element
int *y = &a[9]; // last element
}
Output
Add of a[0]: 140729162482768 add of a[9]: 140729162482804
Subtraction of two pointers: 9
It can be seen that the numerical difference between the two integers is 36; it suggests that the
subtraction is 9, because it can accommodate 9 integers between the two pointers.
Comparison of Pointers
● 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.
Example of Comparing Pointers
In the following example, we are declaring two pointers and initializing them with the first and
last elements of the array respectively. We will keep incrementing the first variable pointer as
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]" (i.e., the second pointer).
#include <stdio.h>
int main() {
int var[] = {10, 100, 200};
10
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)
// Initializing pointers
ptr1 = var;
ptr2 = &var[MAX - 1];
return 0;
}
Output
11