SlideShare a Scribd company logo
Introduction to Computers and
Programming in C
Module V
Pointers
1
Pointers
Pointer declaration:
A pointer is a variable that contains the memory location of
another variable. We start by specifying the type of data stored
in the location identified by the pointer. The asterisk tells the
compiler that we are creating a pointer variable. Finally we
give the name of the variable.
The syntax is as shown below.
type *variable_name
Example:
int *ptr;
float *ptr1;
2
Address Operator
Once we declare a pointer variable, we must point it to something
we can do this by assigning to the pointer the address of the
variable you want to point as in the following example:
ptr=#
Another Example
int i=3;
• Reserve space in memory to hold the integer value
• Associate the name i with this memory location.
• Store the value 3 at this location
3
Pointers
4
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointers
5
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Indirection Operator
6
Value at address pointer (*)-Returns the
value stored at a particular address
(indirection operator).
Printing the value of *(&i) is same as printing
the value of i.
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointers
7
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointers
8
• Pointers are variables that contain
addresses and since addresses are
always whole numbers, pointers always
contain whole numbers.
Pointers
9
❖float *s
• The declaration float *s does not mean
that s is going to contain a floating-point
value. It means that s is going to contain
the address of a floating-point value.
Pointers
10
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointers
11
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Jargon of Pointers
12
int a=35;
int *b;
b=&a;
• b contains address of an int
• Value at address contained in b is an int
• b is an int pointer
• b points to an int
• b is a pointer which points in direction of
an int.
/* Program to display the contents of the variable their address using pointer
variable*/
#include<stdio.h>
void main()
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch='a';
intptr=&num;
cptr=&ch;
floptr=&x;
printf("Num %d stored at address %un",*intptr,intptr);
printf("Value %f stored at address %un",*floptr,floptr);
printf("Character %c stored at address %un",*cptr,cptr);
}
char,int and float pointer
main()
{
char c,*cc;
int i, *ii;
float a,*aa;
c=‘A’ //ASCII value stored in c
i=54;
a=3.14;
cc=&c;ii=&i;aa=&a;
Printf(“address contained in cc = %u", cc);
Printf(“address contained in ii = %u", ii);
Print(“address contained in aa = %u", aa);
printf(“value of c=%c”, *cc);
} 14
char,int and float pointer
15
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Like other variables pointer variables can be used in expressions.
For example if p1 and p2 are properly declared and initialized pointers, then
the following statements are valid.
y = *p1 * *p2;
sum = sum + *p1;
z = 5 - *p2/*p1;
*p2 = *p2 + 10;
we can also compare pointers by using relational operators the expressions such
as
p1 >p2 , p1==p2 and p1!=p2 are allowed.
Pointer expressions &
pointer arithmetic
/*Program to illustrate the pointer expression and pointer arithmetic*/
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr1,*ptr2;
int a,b,x,y,z;
clrscr();
a=30;b=6;
ptr1=&a;
ptr2=&b;
x=*ptr1+ *ptr2-6; /* 30+6-6 =30 */
y=6* (- *ptr1) / *ptr2 + 30; /* 6*(-30)/6+30=0 */
printf("nAddress of a %u",ptr1);
printf("nAddress of b %u",ptr2);
printf("na=%d, b=%d",a,b);
printf("nx=%d,y=%d",x,y);
ptr1=ptr1 + 70;
printf("na=%d, b=%d",a,b);
printf("nAddress of a %u",ptr1);
getch();
}
Passing address to functions
Arguments are generally passed to functions
in one of the two ways
• Sending the value of the arguments
• Sending the addresses of the arguments
18
Sending the values of the
arguments
• ‘value’ of each actual argument in the
calling function is copied into
corresponding formal argument of the
called function.
• Changes made to the formal arguments in
the called function have no effect on the
values of actual arguments in the calling
function.
19
Call by value
main()
{
int a =10, b=20;
Swap(a,b);
Printf(“na=%d”,a);
Printf(“n b=%d”,b);
}
Swap(int x, int y)
{
int t;
t=x;
x=y;y=t;
printf(“nx=%d“, x);
printf (“ny = %d",y );
} 20
Call by reference
• The address of actual arguments in the
calling function are copied into formal
arguments of the called function.
• Using formal arguments in the called
function changes can be made in the
actual arguments of the calling function.
21
Call by Reference
22
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Call by reference
• Using call by reference function can return
more than one value at a time.
main()
{int radius; float area, perimeter;
Printf(“Enter the radius of a circle”);
scanf(“%d”,&radius);
Areaperi(radius,&area,&perimeter);
printf(%f %f,area,perimeter);
}
Areaperi (int r,float *a,float *p)
{
*a= 3.14*r*r;
*p= 2*3.14*r;
}
23
Functions Returning Pointers
main()
{
Int *p;
Int *fun(); // prototype declaration
p=fun();
printf(“n%u”,p);
printf(“n%d”,*p);
}
Int *fun() //function definition
{
int i=20;
return(&i);
}
24
*ptr++ and ++*ptr
• *ptr++ increments the pointer not the value
pointed by it.
• ++*ptr increments the value being pointed
to by ptr.
25
Void pointer
main()
{
int a=10, *j;
void *k;
j=k=&a;
j++;k++;
printf(“n %u %u”,j,k);
}
26
Passing Array Elements
• Array elements can be passed to a
function by calling the function
(a)By value
(b)By reference
27
Passing Array ELements
Call by value Call by reference
28
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointers and Arrays
main()
{
int i=3, *x;
float j=1.5, *y;
char k= ‘c’,*z;
printf(“%d%f%c”,i,j,k);
x=&i,y=&j,z=&k;
printf(%u%u%u”,x,y,z);
x++;y++;z++;
printf(%u%u%u”,x,y,z);
}
29
Pointers and Arrays
• Do not attempt the following operations on
Pointers
Addition of two pointers
Multiplying pointer with a number
Dividing a pointer with a number
int i=4,*j,*k;
j=&i;
j=j+1,j=j-5; //Allowed
30
Accessing Array Elements
main()
{
int num[]={24,34,12,44,56,17};
int i=0;
while(i<=5)
{
printf(“n address is %u”,&num[i]);
printf(“element=%d”,num[i]);
i++;
}
}
31
Accessing Array Elements-
using pointer
main()
{
int num[]={24,34,12,44,56,17};
int i=0, *j;
j=&num[0];
while(i<=5)
{
printf(“n address is %u”,&num[i]);
printf(“element=%d”,*j);
i++;
j++; // increment pointer to the next location
}
}
32
Passing an Entire Array to a
function
main()
{
int num[]={24,34,12,44,56,17};
display(&num[0],6); // or display(num,6);
}
display( int *j, int n)
{ int i=1;
While(i<=n)
{
printf(“element=%d”,*j);
i++;
j++; // increment pointer to point the next location
}
33
Accessing array element in
different ways
34
Source: Let Us C, Yashvant Kanetkar, BPB Publications
2-D Array
35
2-D Array using Pointer
36
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Pointer to an array
• As we have pointer to int,float and char we
can have pointer to an array
• For example int (*q)[4] means that q is a
pointer to an array of 4 integers
37
Pointer to an array
38
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Passing 2-D array to a function
39
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Passing 2-D array to a function
40
Source: Let Us C, Yashvant Kanetkar, BPB Publications
Returning Array from Function
• A pointer to an integer
• A pointer to the zeroth 1-D array
• A pointer to the 2-D array
41
Array of Pointers
• A pointer variable always contains an
address, an array of pointers would be
nothing but collection of addresses.
Main()
{
Static int a[]={0,1,2,34};
Static int *p[]={a,a+1,a+2,a+3,a+4};
printf(“n%u%u%d”,p,*p,*(*p));
}
42
Dynamic Menory Allocation
• If we want to allocate memory at the time of
excecution then it can be done using standard library
functions calloc() and malloc() and are known as
dynamic memory allocation functions.
• In C, malloc is a memory allocation function that
dynamically allocates a large block of memory with a
specified size. It's useful when you don't know the
amount of memory needed during compile time.
• The calloc() function in C is used to allocate multiple
blocks of memory that are the same size. It's a
dynamic memory allocation function that sets each
block to zero. calloc() stands for "contiguous
allocation". 43
Example
int main()
{
int n,avg,i,*p,sum=0;
printf("nEnter the number of students");
scanf("%d",&n);
p=(int *)malloc(n*2);
if(p==NULL)
{
printf("Memory allocation unsuccessful");
exit(0);
}
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
{
sum=sum+*(p+i);
}
avg=sum/n;
printf("Avarage marks=%d",avg);
return 0;
} 44
Example problem
45
Source: Let Us C, Yashvant Kanetkar, BPB Publications
References
• Programming in ANSI C, E. Balagurusamy. McGrawHill
• Let Us C, Yashvant Kanetkar, BPB Publications
• Programming in C, Reema Thareja, Oxford University Press
• https://www.dotnettricks.com
46
Thank You

More Related Content

Similar to Pointers.pdf

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
Koganti Ravikumar
 
Pointers
PointersPointers
Pointers
Abhimanyu Mehta
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
Rubal Bansal
 
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
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
thenmozhip8
 
U3.pptx
U3.pptxU3.pptx
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
SIMONTHOMAS S
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
SwapnaliPawar27
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
sangeeta borde
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
Thesis Scientist Private Limited
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
rajatryadav22
 

Similar to Pointers.pdf (20)

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointers
PointersPointers
Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
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
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

Pointers.pdf

  • 1. Introduction to Computers and Programming in C Module V Pointers 1
  • 2. Pointers Pointer declaration: A pointer is a variable that contains the memory location of another variable. We start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that we are creating a pointer variable. Finally we give the name of the variable. The syntax is as shown below. type *variable_name Example: int *ptr; float *ptr1; 2
  • 3. Address Operator Once we declare a pointer variable, we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following example: ptr=&num; Another Example int i=3; • Reserve space in memory to hold the integer value • Associate the name i with this memory location. • Store the value 3 at this location 3
  • 4. Pointers 4 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 5. Pointers 5 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 6. Indirection Operator 6 Value at address pointer (*)-Returns the value stored at a particular address (indirection operator). Printing the value of *(&i) is same as printing the value of i. Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 7. Pointers 7 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 8. Pointers 8 • Pointers are variables that contain addresses and since addresses are always whole numbers, pointers always contain whole numbers.
  • 9. Pointers 9 ❖float *s • The declaration float *s does not mean that s is going to contain a floating-point value. It means that s is going to contain the address of a floating-point value.
  • 10. Pointers 10 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 11. Pointers 11 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 12. Jargon of Pointers 12 int a=35; int *b; b=&a; • b contains address of an int • Value at address contained in b is an int • b is an int pointer • b points to an int • b is a pointer which points in direction of an int.
  • 13. /* Program to display the contents of the variable their address using pointer variable*/ #include<stdio.h> void main() { int num, *intptr; float x, *floptr; char ch, *cptr; num=123; x=12.34; ch='a'; intptr=&num; cptr=&ch; floptr=&x; printf("Num %d stored at address %un",*intptr,intptr); printf("Value %f stored at address %un",*floptr,floptr); printf("Character %c stored at address %un",*cptr,cptr); }
  • 14. char,int and float pointer main() { char c,*cc; int i, *ii; float a,*aa; c=‘A’ //ASCII value stored in c i=54; a=3.14; cc=&c;ii=&i;aa=&a; Printf(“address contained in cc = %u", cc); Printf(“address contained in ii = %u", ii); Print(“address contained in aa = %u", aa); printf(“value of c=%c”, *cc); } 14
  • 15. char,int and float pointer 15 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 16. Like other variables pointer variables can be used in expressions. For example if p1 and p2 are properly declared and initialized pointers, then the following statements are valid. y = *p1 * *p2; sum = sum + *p1; z = 5 - *p2/*p1; *p2 = *p2 + 10; we can also compare pointers by using relational operators the expressions such as p1 >p2 , p1==p2 and p1!=p2 are allowed. Pointer expressions & pointer arithmetic
  • 17. /*Program to illustrate the pointer expression and pointer arithmetic*/ #include<stdio.h> #include<conio.h> void main() { int *ptr1,*ptr2; int a,b,x,y,z; clrscr(); a=30;b=6; ptr1=&a; ptr2=&b; x=*ptr1+ *ptr2-6; /* 30+6-6 =30 */ y=6* (- *ptr1) / *ptr2 + 30; /* 6*(-30)/6+30=0 */ printf("nAddress of a %u",ptr1); printf("nAddress of b %u",ptr2); printf("na=%d, b=%d",a,b); printf("nx=%d,y=%d",x,y); ptr1=ptr1 + 70; printf("na=%d, b=%d",a,b); printf("nAddress of a %u",ptr1); getch(); }
  • 18. Passing address to functions Arguments are generally passed to functions in one of the two ways • Sending the value of the arguments • Sending the addresses of the arguments 18
  • 19. Sending the values of the arguments • ‘value’ of each actual argument in the calling function is copied into corresponding formal argument of the called function. • Changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. 19
  • 20. Call by value main() { int a =10, b=20; Swap(a,b); Printf(“na=%d”,a); Printf(“n b=%d”,b); } Swap(int x, int y) { int t; t=x; x=y;y=t; printf(“nx=%d“, x); printf (“ny = %d",y ); } 20
  • 21. Call by reference • The address of actual arguments in the calling function are copied into formal arguments of the called function. • Using formal arguments in the called function changes can be made in the actual arguments of the calling function. 21
  • 22. Call by Reference 22 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 23. Call by reference • Using call by reference function can return more than one value at a time. main() {int radius; float area, perimeter; Printf(“Enter the radius of a circle”); scanf(“%d”,&radius); Areaperi(radius,&area,&perimeter); printf(%f %f,area,perimeter); } Areaperi (int r,float *a,float *p) { *a= 3.14*r*r; *p= 2*3.14*r; } 23
  • 24. Functions Returning Pointers main() { Int *p; Int *fun(); // prototype declaration p=fun(); printf(“n%u”,p); printf(“n%d”,*p); } Int *fun() //function definition { int i=20; return(&i); } 24
  • 25. *ptr++ and ++*ptr • *ptr++ increments the pointer not the value pointed by it. • ++*ptr increments the value being pointed to by ptr. 25
  • 26. Void pointer main() { int a=10, *j; void *k; j=k=&a; j++;k++; printf(“n %u %u”,j,k); } 26
  • 27. Passing Array Elements • Array elements can be passed to a function by calling the function (a)By value (b)By reference 27
  • 28. Passing Array ELements Call by value Call by reference 28 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 29. Pointers and Arrays main() { int i=3, *x; float j=1.5, *y; char k= ‘c’,*z; printf(“%d%f%c”,i,j,k); x=&i,y=&j,z=&k; printf(%u%u%u”,x,y,z); x++;y++;z++; printf(%u%u%u”,x,y,z); } 29
  • 30. Pointers and Arrays • Do not attempt the following operations on Pointers Addition of two pointers Multiplying pointer with a number Dividing a pointer with a number int i=4,*j,*k; j=&i; j=j+1,j=j-5; //Allowed 30
  • 31. Accessing Array Elements main() { int num[]={24,34,12,44,56,17}; int i=0; while(i<=5) { printf(“n address is %u”,&num[i]); printf(“element=%d”,num[i]); i++; } } 31
  • 32. Accessing Array Elements- using pointer main() { int num[]={24,34,12,44,56,17}; int i=0, *j; j=&num[0]; while(i<=5) { printf(“n address is %u”,&num[i]); printf(“element=%d”,*j); i++; j++; // increment pointer to the next location } } 32
  • 33. Passing an Entire Array to a function main() { int num[]={24,34,12,44,56,17}; display(&num[0],6); // or display(num,6); } display( int *j, int n) { int i=1; While(i<=n) { printf(“element=%d”,*j); i++; j++; // increment pointer to point the next location } 33
  • 34. Accessing array element in different ways 34 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 36. 2-D Array using Pointer 36 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 37. Pointer to an array • As we have pointer to int,float and char we can have pointer to an array • For example int (*q)[4] means that q is a pointer to an array of 4 integers 37
  • 38. Pointer to an array 38 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 39. Passing 2-D array to a function 39 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 40. Passing 2-D array to a function 40 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 41. Returning Array from Function • A pointer to an integer • A pointer to the zeroth 1-D array • A pointer to the 2-D array 41
  • 42. Array of Pointers • A pointer variable always contains an address, an array of pointers would be nothing but collection of addresses. Main() { Static int a[]={0,1,2,34}; Static int *p[]={a,a+1,a+2,a+3,a+4}; printf(“n%u%u%d”,p,*p,*(*p)); } 42
  • 43. Dynamic Menory Allocation • If we want to allocate memory at the time of excecution then it can be done using standard library functions calloc() and malloc() and are known as dynamic memory allocation functions. • In C, malloc is a memory allocation function that dynamically allocates a large block of memory with a specified size. It's useful when you don't know the amount of memory needed during compile time. • The calloc() function in C is used to allocate multiple blocks of memory that are the same size. It's a dynamic memory allocation function that sets each block to zero. calloc() stands for "contiguous allocation". 43
  • 44. Example int main() { int n,avg,i,*p,sum=0; printf("nEnter the number of students"); scanf("%d",&n); p=(int *)malloc(n*2); if(p==NULL) { printf("Memory allocation unsuccessful"); exit(0); } for(i=0;i<n;i++) { scanf("%d",(p+i)); } for(i=0;i<n;i++) { sum=sum+*(p+i); } avg=sum/n; printf("Avarage marks=%d",avg); return 0; } 44
  • 45. Example problem 45 Source: Let Us C, Yashvant Kanetkar, BPB Publications
  • 46. References • Programming in ANSI C, E. Balagurusamy. McGrawHill • Let Us C, Yashvant Kanetkar, BPB Publications • Programming in C, Reema Thareja, Oxford University Press • https://www.dotnettricks.com 46