SlideShare a Scribd company logo
1
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
 A derived data type
 A pointer is used when we want to know where the
data is stored in computer memory
 Pointer variables are the special variables which are
used to store the address of other variables i.e. direct
address of the memory location
 Pointer variable contains memory address as their
values
2
For example-
int a=15; a
3
15
110
Memory Representation
Variable_name
Variable_value
Variable_address/
Memory_location
Note: Here we showed memory representation of variable initialization i.e. int
a=15. Now from the picture, it is clear that we can access the value of a variable
with their name as well as their address also. So, if we use the address to access
variables than we use a pointer.
 The computer memory is viewed as an array of consecutive
memory locations
 Each memory location is given a unique identification
number called address
 Finally, A pointer variable is a variable that can hold the
address of the other variable (same type)
 For example- if we want to store the address of integer
variable than the pointer variable must be of type int.
 Two pointer operators are used such as - & (address-of) and
* (De-referencing) operator
4
 A pointer variable must be declared like other variables
before using them in a program
◦ Syntax to declare variables
 data_type variable_names;
 For example- int a,b;
◦ Syntax to declare pointer variable
 data_type *variable_names;
 For example- int *a, *b; or int* a,b;
5
 To assign value of a variable
◦ Syntax
 variable_name=value;
 For example- a=20;
 To assign address of a pointer variable
◦ Syntax
 pointer_variable_name=&variable_name;
 For example- ptr=&a;
 To get value from a pointer variable
 Apply * to the pointer variable i.e. *ptr
6
For example-
a
int a=20;
int *ptr;
ptr=&a; // i.e. address of a assign to ptr
If we print the value of ptr than we get the address of variable
‘a’.
printf(“%d”,ptr); // output will be 100
If we want to print the value of the variable whose address
stored by the pointer variable then we apply * to the pointer
variable.
printf (“%d”,*ptr); // output will be 20
7
20
100
100
ptr
 Like other variables, pointer variables are also
initialized
◦ Syntax to initialized other variables
 data_types variable_names=values;
 int a=5;
 int x[5]={10,20,30,40,50};
◦ Syntax to initialized pointer variable
 Data_type *variable_name=&variable_name;
 int *ptr=&a;
8
 WAP to print address and value of a variable using pointer variable
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
int *ptr;
clrscr();
printf("Enter a numbern");
scanf("%d",&x);
ptr=&x;
printf("The address of x variable is %dn",ptr);
printf("The value of x variable is %d",*ptr);
getch();
}
Output:-
9
Hear we are going to discuss basic operation performed
by the pointers
 Assignments
 Arithmetic operations
◦ Addition
◦ Subtraction
 Comparison
10
 A pointer variable cab be assign the address of the other
variable. For example
int a,*ptr;
ptr=&a;
 If two pointer variables are pointed to the object of
same type than they can be assign. For example
int a, *ptr1, *ptr2;
ptr1=&a;
ptr2=ptr1;
11
 A pointer variable can be assign a null value. For
example
int *ptr;
ptr=‘0’;
Note- A pointer that is not assigned any value. If we don't
have any address to be specified in the pointer at the
time of declaration then we can assign NULL value.
int *ptr=NULL;
In most libraries, the value of the pointer is 0 (zero).
12
 We can perform arithmetic operations on pointers.
Following are the possible arithmetic operations
◦ Addition
◦ Subtraction
13
 When we increase a pointer by 1 the pointer begins
pointing to the next position immediately.
 The pointer value would be raised by the size of the
type of data to which the pointer is pointing.
 By using the addition operation on a pointer that keeps
pointing to every element of the array, performing
some operation on it, and updating itself in a loop, we
can traverse an array.
14
For example
int a[5]={10,11,12,13,14};
int *ptr;
Ptr=&a or &a[0];
ptr++;
or
ptr=ptr+1 Base Address
Gives address of the next element in a list
i.e. here the new address will be 102
New_address= current_address + n*size_of(data_type)
Where n is the number by which the pointer get incresed
15
Memory Representation
ptr=ptr+3
= &a + 3* size_of(int)
= 100+3*2
= 106
It denotes address of a[3] element in the array.
16
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]={10,11,12,13,14};
int *ptr;//pointer to int
clrscr();
ptr=&a[0];//stores the address of n variable
printf("Address of ptr variable is %u n",ptr);
ptr=ptr+1;
printf("After increment by 1: Address of ptr variable is %u n",ptr);
ptr=ptr+3;
printf("After increment by 3: Address of ptr variable is %u n",ptr);
getch();
return 0;
}
17
Output:
Here, the base address allocated by compiler is 65516.
Note: First address of the array is called base address of
the array.
18
 We may deduct a value from the pointer variable, like
pointer addition.
 An address will be given by subtracting any number
from a pointer.
 The value subtraction formula from the pointer variable
is given below:
new_address= current_address - (number*size_of(data_type))
19
#include<stdio.h>
#include<conio.h>
void main()
{
int num=50;
int *ptr;//pointer to int
clrscr();
ptr=&num;//stores the address of number variable
printf("Address of ptr variable is %u n",ptr);
ptr=ptr-2; //subtracting 3 from pointer variable
printf("After subtracting 2: Address of ptr variable is %u n",ptr);
getch();
}
20
 Address + Address
 Address * Address
 Address % Address
 Address / Address
 Address & Address
 Address ^ Address
 Address | Address
 ~Address
21
 Pointers can be compared by the use of relational
operators like = =, <, and >. If two pointer variables are
pointing to the object of the same data type then they
can be compared with one another.
 For example,
int *ptr1,*ptr2;
int x=10;
ptr1=&x;
Then we can use relational operators
if (ptr1>ptr2) and so on
22
 The pointer reduces the code and enhances the
performance, it is used with arrays, structures, and
functions to retrieve strings, trees, etc.
 Using the pointer we can return multiple values from
one method.
 It lets us access any memory location in the memory of
our machine.
23
 Many pointer implementations are in c language.
 Dynamic allocation of memory
We can dynamically allocate memory in c language
using malloc) (and calloc) (functions where the pointer
is used.
 Arrays, Functions and Structures
Pointers are commonly used in arrays, functions, and
structures in c language. Reduces code and improves
performance.
24
 A normal variable stores the value whereas the pointer variable stores
the address of the variable.
 The content of the C pointer always is a whole number i.e. address.
 Always C pointer is initialized to null, i.e. int *p = NULL.
 The value of the NULL pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is
pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are
available between these two pointers.
 But, Pointer addition, multiplication, division are not allowed.
 The size of any pointer is 2 byte (for 16-bit compiler).
25
26

More Related Content

What's hot

Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Linked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptxLinked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptx
Santhiya S
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
Dhrumil Patel
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 

What's hot (20)

Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Linked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptxLinked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptx
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
C Pointers
C PointersC Pointers
C Pointers
 
Structure & union
Structure & unionStructure & union
Structure & union
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 

Similar to Pointers in c

Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
Md. Afif Al Mamun
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
Thesis Scientist Private Limited
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
Krishna Nanda
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 

Similar to Pointers in c (20)

Pointer in C
Pointer in CPointer in C
Pointer in C
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Pointers
PointersPointers
Pointers
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 

More from CHANDAN KUMAR

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
CHANDAN KUMAR
 
Raid technology
Raid technologyRaid technology
Raid technology
CHANDAN KUMAR
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
CHANDAN KUMAR
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
CHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 

More from CHANDAN KUMAR (12)

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
Raid technology
Raid technologyRaid technology
Raid technology
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Linked List
Linked ListLinked List
Linked List
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Pointers in c

  • 1. 1 Prepared By: Dr. Chandan Kumar Assistant Professor, Computer Science & Engineering Department Invertis University, Bareilly
  • 2.  A derived data type  A pointer is used when we want to know where the data is stored in computer memory  Pointer variables are the special variables which are used to store the address of other variables i.e. direct address of the memory location  Pointer variable contains memory address as their values 2
  • 3. For example- int a=15; a 3 15 110 Memory Representation Variable_name Variable_value Variable_address/ Memory_location Note: Here we showed memory representation of variable initialization i.e. int a=15. Now from the picture, it is clear that we can access the value of a variable with their name as well as their address also. So, if we use the address to access variables than we use a pointer.
  • 4.  The computer memory is viewed as an array of consecutive memory locations  Each memory location is given a unique identification number called address  Finally, A pointer variable is a variable that can hold the address of the other variable (same type)  For example- if we want to store the address of integer variable than the pointer variable must be of type int.  Two pointer operators are used such as - & (address-of) and * (De-referencing) operator 4
  • 5.  A pointer variable must be declared like other variables before using them in a program ◦ Syntax to declare variables  data_type variable_names;  For example- int a,b; ◦ Syntax to declare pointer variable  data_type *variable_names;  For example- int *a, *b; or int* a,b; 5
  • 6.  To assign value of a variable ◦ Syntax  variable_name=value;  For example- a=20;  To assign address of a pointer variable ◦ Syntax  pointer_variable_name=&variable_name;  For example- ptr=&a;  To get value from a pointer variable  Apply * to the pointer variable i.e. *ptr 6
  • 7. For example- a int a=20; int *ptr; ptr=&a; // i.e. address of a assign to ptr If we print the value of ptr than we get the address of variable ‘a’. printf(“%d”,ptr); // output will be 100 If we want to print the value of the variable whose address stored by the pointer variable then we apply * to the pointer variable. printf (“%d”,*ptr); // output will be 20 7 20 100 100 ptr
  • 8.  Like other variables, pointer variables are also initialized ◦ Syntax to initialized other variables  data_types variable_names=values;  int a=5;  int x[5]={10,20,30,40,50}; ◦ Syntax to initialized pointer variable  Data_type *variable_name=&variable_name;  int *ptr=&a; 8
  • 9.  WAP to print address and value of a variable using pointer variable #include<stdio.h> #include<conio.h> void main() { int x; int *ptr; clrscr(); printf("Enter a numbern"); scanf("%d",&x); ptr=&x; printf("The address of x variable is %dn",ptr); printf("The value of x variable is %d",*ptr); getch(); } Output:- 9
  • 10. Hear we are going to discuss basic operation performed by the pointers  Assignments  Arithmetic operations ◦ Addition ◦ Subtraction  Comparison 10
  • 11.  A pointer variable cab be assign the address of the other variable. For example int a,*ptr; ptr=&a;  If two pointer variables are pointed to the object of same type than they can be assign. For example int a, *ptr1, *ptr2; ptr1=&a; ptr2=ptr1; 11
  • 12.  A pointer variable can be assign a null value. For example int *ptr; ptr=‘0’; Note- A pointer that is not assigned any value. If we don't have any address to be specified in the pointer at the time of declaration then we can assign NULL value. int *ptr=NULL; In most libraries, the value of the pointer is 0 (zero). 12
  • 13.  We can perform arithmetic operations on pointers. Following are the possible arithmetic operations ◦ Addition ◦ Subtraction 13
  • 14.  When we increase a pointer by 1 the pointer begins pointing to the next position immediately.  The pointer value would be raised by the size of the type of data to which the pointer is pointing.  By using the addition operation on a pointer that keeps pointing to every element of the array, performing some operation on it, and updating itself in a loop, we can traverse an array. 14
  • 15. For example int a[5]={10,11,12,13,14}; int *ptr; Ptr=&a or &a[0]; ptr++; or ptr=ptr+1 Base Address Gives address of the next element in a list i.e. here the new address will be 102 New_address= current_address + n*size_of(data_type) Where n is the number by which the pointer get incresed 15 Memory Representation
  • 16. ptr=ptr+3 = &a + 3* size_of(int) = 100+3*2 = 106 It denotes address of a[3] element in the array. 16
  • 17. #include<stdio.h> #include<conio.h> int main() { int a[5]={10,11,12,13,14}; int *ptr;//pointer to int clrscr(); ptr=&a[0];//stores the address of n variable printf("Address of ptr variable is %u n",ptr); ptr=ptr+1; printf("After increment by 1: Address of ptr variable is %u n",ptr); ptr=ptr+3; printf("After increment by 3: Address of ptr variable is %u n",ptr); getch(); return 0; } 17
  • 18. Output: Here, the base address allocated by compiler is 65516. Note: First address of the array is called base address of the array. 18
  • 19.  We may deduct a value from the pointer variable, like pointer addition.  An address will be given by subtracting any number from a pointer.  The value subtraction formula from the pointer variable is given below: new_address= current_address - (number*size_of(data_type)) 19
  • 20. #include<stdio.h> #include<conio.h> void main() { int num=50; int *ptr;//pointer to int clrscr(); ptr=&num;//stores the address of number variable printf("Address of ptr variable is %u n",ptr); ptr=ptr-2; //subtracting 3 from pointer variable printf("After subtracting 2: Address of ptr variable is %u n",ptr); getch(); } 20
  • 21.  Address + Address  Address * Address  Address % Address  Address / Address  Address & Address  Address ^ Address  Address | Address  ~Address 21
  • 22.  Pointers can be compared by the use of relational operators like = =, <, and >. If two pointer variables are pointing to the object of the same data type then they can be compared with one another.  For example, int *ptr1,*ptr2; int x=10; ptr1=&x; Then we can use relational operators if (ptr1>ptr2) and so on 22
  • 23.  The pointer reduces the code and enhances the performance, it is used with arrays, structures, and functions to retrieve strings, trees, etc.  Using the pointer we can return multiple values from one method.  It lets us access any memory location in the memory of our machine. 23
  • 24.  Many pointer implementations are in c language.  Dynamic allocation of memory We can dynamically allocate memory in c language using malloc) (and calloc) (functions where the pointer is used.  Arrays, Functions and Structures Pointers are commonly used in arrays, functions, and structures in c language. Reduces code and improves performance. 24
  • 25.  A normal variable stores the value whereas the pointer variable stores the address of the variable.  The content of the C pointer always is a whole number i.e. address.  Always C pointer is initialized to null, i.e. int *p = NULL.  The value of the NULL pointer is 0.  & symbol is used to get the address of the variable.  * symbol is used to get the value of the variable that the pointer is pointing to.  If a pointer in C is assigned to NULL, it means it is pointing to nothing.  Two pointers can be subtracted to know how many elements are available between these two pointers.  But, Pointer addition, multiplication, division are not allowed.  The size of any pointer is 2 byte (for 16-bit compiler). 25
  • 26. 26