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

PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPTX
Pointer in C
PPT
l7-pointers.ppt
PPTX
Pointers in C Language
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPTX
Introduction to pointers in c plus plus .
PPTX
4 Pointers.pptx
PPTX
Pointers in C++ object oriented programming
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Pointer in C
l7-pointers.ppt
Pointers in C Language
Algoritmos e Estruturas de Dados - Pointers
Introduction to pointers in c plus plus .
4 Pointers.pptx
Pointers in C++ object oriented programming

Similar to Pointers.pdf (20)

PPTX
Pointers
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPSX
Pointers
PPTX
C Programming Unit-4
PPTX
Unit-I Pointer Data structure.pptx
PDF
PSPC--UNIT-5.pdf
PPTX
PPS-POINTERS.pptx
PPT
Advanced pointers
PPTX
Pointers
PPTX
pointers.pptx
PPTX
COM1407: Working with Pointers
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPT
Pointers+(2)
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
unit 3 ppt.pptx
PPT
SPC Unit 3
PPTX
UNIT 6structureofcprogrammingforppt.pptx
PPTX
Pointer.pptx
Pointers
Pointers and single &multi dimentionalarrays.pptx
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Pointers
C Programming Unit-4
Unit-I Pointer Data structure.pptx
PSPC--UNIT-5.pdf
PPS-POINTERS.pptx
Advanced pointers
Pointers
pointers.pptx
COM1407: Working with Pointers
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointers+(2)
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
unit 3 ppt.pptx
SPC Unit 3
UNIT 6structureofcprogrammingforppt.pptx
Pointer.pptx
Ad

Recently uploaded (20)

PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Hazard Identification & Risk Assessment .pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
advance database management system book.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
IGGE1 Understanding the Self1234567891011
PDF
Empowerment Technology for Senior High School Guide
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Trump Administration's workforce development strategy
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Classroom Observation Tools for Teachers
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
LDMMIA Reiki Yoga Finals Review Spring Summer
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Hazard Identification & Risk Assessment .pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
advance database management system book.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Final Presentation General Medicine 03-08-2024.pptx
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Digestion and Absorption of Carbohydrates, Proteina and Fats
IGGE1 Understanding the Self1234567891011
Empowerment Technology for Senior High School Guide
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Complications of Minimal Access Surgery at WLH
Trump Administration's workforce development strategy
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Classroom Observation Tools for Teachers
Indian roads congress 037 - 2012 Flexible pavement
Supply Chain Operations Speaking Notes -ICLT Program
Computing-Curriculum for Schools in Ghana
Ad

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