0% found this document useful (0 votes)
3 views

Unit 3 Note 1 Pointer

The document provides an introduction to pointers in C, explaining their syntax, advantages, disadvantages, and differences from arrays. It covers pointer arithmetic, including incrementing, decrementing, and comparing pointers, along with examples of their usage. Additionally, it discusses pointer initialization and traversal using pointers in arrays.

Uploaded by

omdas4106
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 Note 1 Pointer

The document provides an introduction to pointers in C, explaining their syntax, advantages, disadvantages, and differences from arrays. It covers pointer arithmetic, including incrementing, decrementing, and comparing pointers, along with examples of their usage. Additionally, it discusses pointer initialization and traversal using pointers in arrays.

Uploaded by

omdas4106
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIT-3

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

Following are the major advantages of pointers in C:


• Pointers are used for dynamic memory allocation and deallocation.

• An Array or a structure can be accessed efficiently with pointers

• Pointers are useful for accessing memory locations.

• 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

Pointers are vulnerable to errors and have the following disadvantages:


• Memory corruption can occur if an incorrect value is provided to pointers.

• Pointers are a little bit complex to understand.

• Pointers are comparatively slower than variables in C.

• Uninitialized pointers might cause a segmentation fault.


Differences and Similarities between an array and a pointer :

Pointer Array

A pointer is a derived data type that can An array is a homogeneous collection of


store the address of other variables. items of any type such as int, char, etc.

Pointers are allocated at run time. Arrays are allocated at runtime.

An array is a collection of variables of the


The pointer is a single variable.
same type.

Dynamic in Nature Static in Nature.


Initialization of Pointer

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

2.Addition of integer to a pointer

3.Subtraction of integer to a pointer

4.Subtracting two pointers of the same type

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.

Step 3: Display the output.


#include<stdio.h>
int main()
{
int arr[5];
int *ptr1 = &arr;
int *ptr2 = &arr[0];
printf("ptr1=%u\n",ptr1);
printf("ptr2=%u\n",ptr2);
if (ptr1==ptr2)
printf("First Element are Equal.");
else
printf("First Element are not Equal.");
return 0;
}
Output:
ptr1=6487536
ptr2=6487536
// C Program to demonstrate the pointer comparison with NULL
#include <stdio.h>
int main()
{
int* ptr = NULL;
if (ptr == NULL)
printf("The pointer is NULL");
else
printf("The pointer is not NULL");
return 0;
}
Output:
The pointer is NULL
// traversal using pointers
#include <stdio.h>
int main()
{
int N = 5,i;
int arr[] = { 1, 2, 3, 4, 5 };
int* ptr;
ptr = arr;
for (i = 0; i < N; i++) {
printf("%d", ptr[i]);
}
}
/* output-12345 */
Pointer Arithmetic on Arrays

// traversal using pointers


#include <stdio.h>
int main()
{
int N = 5,i;
int arr[] = { 1, 2, 3, 4, 5 };
int* ptr;
ptr = arr;
for (i = 0; i < N; i++) {
printf("%d", ptr[0]);
ptr++;
}
}
// traversal using pointers
#include <stdio.h>
int main()
{
int myNumbers[4] = {25, 50, 75, 100};
int *ptr = myNumbers;
int i;
for (i = 0; i < 4; i++) {
printf("%d", *(ptr + i));
}
}
Output: 25 50 75 100
Note:

*ptr means the Value in the array

++*ptr means the Value incremented by one in the array

--*ptr means the Value decremented by one in the array

(*ptr)++ means the Value incremented by one in the array

*ptr++ means points to the next element in the array

*++ptr means points to the next element in the array


THANK YOU

You might also like