Unit 3 Note 1 Pointer
Unit 3 Note 1 Pointer
Introduction to Pointer
Er. Sweta Samantaray
H.O.D
I.T & Comp.Sc
Introduction to Pointer
A pointer is a derived data type that can store the memory address of
other variables, functions, or even pointers.
Syntax of C Pointers
type *ptr;
where,
ptr is the name of the pointer.
type is the type of data it is pointing to(int, char, float, double, array etc.).
The (*) dereferencing operator denotes that the declared variable stores the
address of another variable.
#include <stdio.h>
int main() {
// An integer variable
int a = 10;
// Create a pointer to integer (declaration)
int *ptr;
// Store the address of a inside pointer (initialization)
ptr = &a;
// Print the content of ptr
printf("ptr = %p\n", ptr);
printf("ptr = %u\n", ptr);
// Get the value pointed by ptr (dereferencing)
printf("*ptr = %d", *ptr);
return 0;
}
Output:-
ptr = 000000000062FE14
ptr = 6487572
#include <stdio.h>
int main() {
int x = 10;
int *ptr;
ptr = &x;
printf("x = %p\n", &x);
printf("x = %u\n", &x);
printf("ptr = %p\n", ptr);
printf("ptr = %u\n", ptr);
printf("*ptr = %d\n", *ptr);
printf("*(&x) = %d", *(&x));
return 0;
}
Output:-
x = 000000000062FE14
x = 6487572
ptr = 000000000062FE14
ptr = 6487572
*ptr = 10
Advantages of Pointers
• Pointers are used to form complex data structures such as linked lists,
graphs, trees, etc.
• Pointers reduce the length of the program and its execution time as well.
Disadvantages of Pointers
Pointer Array
int number;
int *ptr=&number;
Is same as
int number;
int *ptr;
ptr =&number;
ILLEGAL assignment in pointer
int *ptr;
float var;
ptr=&var;// pointer of one type can not be assigned to another type
Pointer Arithmetic in C
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on
pointers. The pointer variables store the memory address of another variable. It doesn’t
store any value.
These operations are:
1.Increment/Decrement of a Pointer
5.Comparison of pointers
Increment/Decrement of a Pointer
• When a pointer is incremented, it actually increments by the number equal to the size of the
data type for which it is a pointer.
• For Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by
4(size of an int), and the new address will point to 1004.
• When a pointer is decremented, it actually decrements by the number equal to the size of the
data type for which it is a pointer.
• For Example:
If an integer pointer that stores address 1000 is decremented, then it will decrement by
4(size of an int), and the new address will point to 996.
Addition of Integer to Pointer
When a pointer is added with an integer value, the value is first multiplied by
the size of the data type and then added to the pointer.
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we add integer 5 to it using the expression,
ptr = ptr + 5,
then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.
#include <stdio.h>
int main()
{
int N = 4;
int *ptr1;
ptr1 = &N;
printf("Pointer ptr1 before Addition: ");
printf("%p \n", ptr1);
ptr1 = ptr1 + 3;
printf("Pointer ptr1 after Addition: ");
printf("%p \n", ptr1);
return 0;
}
Output:
Pointer ptr1 before Addition: 000000000062FE14
Pointer ptr1 after Addition : 000000000062FE20
Subtraction of Integer to Pointer
When a pointer is subtracted with an integer value, the value is first multiplied by
the size of the data type and then subtracted from the pointer similar to addition.
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address.
If we subtract integer 5 from it using the expression,
ptr = ptr – 5,
then, the final address stored in the ptr will be
ptr = 1000 – sizeof(int) * 5 = 980.
#include <stdio.h>
int main()
{
int N = 4;
int *ptr1;
ptr1 = &N;
printf("Pointer ptr1 before Subtraction: ");
printf("%p \n", ptr1);
ptr1 = ptr1 - 3;
printf("Pointer ptr1 after Subtraction: ");
printf("%p \n", ptr1);
return 0;
}
OUTPUT:-
Pointer ptr1 before Subtraction: 000000000062FE14
Subtraction of Two Pointers
The subtraction of two pointers is possible only when they have the same data type.
The result is generated by calculating the difference between the addresses of the two
pointers and calculating how many bits of data it is according to the pointer data type. The
subtraction of two pointers gives the increments between the two pointers.
For Example:
Two integer pointers say ptr1(address:1000) and
ptr2(address:1004)
are subtracted.
The difference between addresses is 4 bytes.
Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is given
by (4/4) = 1.
#include <stdio.h>
int main()
{
int x = 6; // Integer variable declaration
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N; // stores address of N
ptr2 = &x; // stores address of x
printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);
x = ptr2 – ptr1;
printf("Subtraction of ptr1 & ptr2 is %d\n",x);
return 0;
}
output:
ptr1 = 6487560, ptr2 = 6487564
Comparison of pointers
We can compare the two pointers by using the comparison operators in C. We can
implement this by using all operators in C >, >=, <, <=, ==, !=.
It returns true for the valid condition and returns false for the unsatisfied condition.
Step 1: Initialize the integer values and point these integer values to the pointer.
Step 2: Now, check the condition by using comparison or relational operators on pointer
variables.