SlideShare a Scribd company logo
Chapter-7 Pointers
Pointers A pointer is a variable that holds a memory address. This address is the location of another object in memory. For example,  if one variable contains the address of another variable, the first variable is said to point to the second. Basically, a pointer contains an address of another variable. Pointers provide an indirect means of accessing or retrieving the data from memory .
Pointers Variable in memory 1000 1001 1002 1003 1004 1005 1006 103 Memory address
The ‘&’ and ‘*’ Consider the declaration  int ab=3; This declaration tells the compiler Reserve space in memory to hold the integer value. Associate the name  ab  with this memory location. Store the value 3 at this location.
The ‘&’ and ‘*’ 3 ab 1000 Location name Value at Location Address
Declaring pointer variable  In C every variable must be declared for its data type. Since pointer variables contain addresses that belong  to a separate data type they must be declared as pointers.  The declaration of pointer variable takes following form:  data_type  *variable_name;  For example:- int  *q;  here q is pointer variable that can hold the address
The ‘&’ and ‘*’ ‘ &’    Address of operator  ‘ *’    Value at address operator. Also called the Indirection Operator. ‘ &a’    Returns the address of variable a. ‘ *a’    Returns the value stored in a particular address.
An Example:Pointers main() { int j=5;  printf(“Address of j=%d\n”,&j);  printf(“Value of j=%d\n”,j); printf(“Value of j=%d\n”,*(&j)); } Output: Address of j = 1000 Value of j = 5 Value of j = 5
#include <stdio.h> int main()  { int a = 10, b = 2; int *p1, *p2; p1 = &a; p2 = &b; printf(&quot;%u %u \n&quot;, p1, p2); printf(&quot;%d %d \n&quot;, *p1, *p2); return 0;  }  A program that prints out pointers and the values they point to:
Write a program that computes the area and perimeter of a rectangle: #include <stdio.h> void rectangle(int a, int b, int  * area, int  * perim); int main() { int x, y; int area, perim; printf(&quot;Enter two values separated by space: &quot; ); scanf(&quot;%d %d&quot;, &x, &y); rectangle(x, y, &area, &perim); printf(&quot;Area is %d Perimeter is %d\n&quot;, area, perim); return 0; } void rectangle(int a,int b,int  * area,int  * perim) { *area = a * b; *perim = 2 * (a + b); }
Pointer Expressions Expressions involving pointers can be classified as follows: Pointer Assignments Pointer Arithmetic Pointer Comparison Pointer Conversion
Pointer Assignments Consider the following example Here i’s value is 5 and j’s value is i’s address. i 5 1000 1000 2000 j
Pointer Assignments Since, the variable j is containing an address, it is declared as  int  *j; This declaration tells the compiler that j will be used to store the address of an integer value – i.e j points to an integer.
An Example:Pointer Assignments main() { int j=3; int *k; k = &j; printf(“Address of j=%d”,&j); printf(“Address of j=%d”,k); printf(“Address of j=%d”,&k); printf(“Value of k=%d”,k); printf(“Value of j=%d”,j); printf(“Value of j=%d”,*(&j)); printf(“Value of j=%d”,*j); } Output: Address of j = 1000 Address of j = 1000 Address of k = 2000 Value of k = 1000 Value of j = 3 Value of j = 3 Value of j = error
Pointer Conversions One type of pointer can be converted to another type of pointer. Consider the following example: main() { double x = 100.1,y; int *p; /*The next statement causes p to point to double*/ p = (int*)&x; y = *p; printf(“The value of x is: %f”,y); }
Pointers v/s Arrays Consider the following program main() { static char arr[10]=“Embedded”; char *s=“Embedded”; printf(“%s\n”,arr); printf(“%s\n”,s); } Output: Embedded Embedded
Pointers v/s Arrays Consider the following program main() { static char arr[10]=“Embedded”; char *s=“Embedded”; s++; printf(“%s\n”,arr); printf(“%s\n”,s); } Output : Embedded   mbedded
Analysis: Pointers v/s Arrays s++  increments  s  such that it starts pointing to  ‘m’  of  Embedded  and hence would print  mbedded  as output. arr++  would give an error message because the information known to us is the base address and  arr++  is attempting to change this base address.
Arrays of Pointers As there are an array of ints, floats, similarly there can be an array of pointers. An array of pointers represent a collection of addresses.
An Example: Arrays of Pointers main() { int *arr[4]; int i=5,j=10,k=15,l=20,m; arr[0]=&i; arr[1]=&j; arr[2]=&k; arr[3]=&l; for(m=0;m<=3;m++) printf(“%d”,*(arr[m])); }
Arrays of Pointers 1000 2500 2000 1500 i 5 10 j 20 15 l k 2500 2000 1500 1000 arr[0] arr[3] arr[2] arr[1] 1200 2700 2200 1700
Pointers To Pointers Pointer as we know is a variable which contains an address of another variable. A pointer which contains an address of another pointer variable can be defined as a  pointer to a pointer  or  DOUBLE POINTER .
Double Pointers 3 i 1000 1000 2000 j 2000 3000 k
Double Pointer main() { int i=3; int *j; int **k; j=&i; k=&j; printf(“Address of i is %d%d%d\n”,&i,j,*k); printf(“Address of j is %d%d\n”,&j,k); printf(“Address of k is %d\n”,&k); printf(“Value of i is %d%d%d%d\n”,i,*(&i),*j,**k); printf(“Value of j is %d\n”,j); printf(“Value of k is %d\n”,k); }
Double Pointer OUTPUT: Address of i is 1000 1000 1000 Address of j is 2000 2000 Address of k is 3000  Value of i is 3 3 3 3 Value of j is 1000 Value of k is 2000
Summary A pointer is a variable that holds a memory address. Basically, a pointer contains an address of another variable. Pointers provide an indirect means of accessing or retrieving the data from memory. Arithmetic operations such as addition, multiplication and division on two pointers are not allowed. A pointer variable cannot be multiplied/divided by a constant or a variable. A pointer which contains an address of another pointer variable can be defined as a  pointer to a pointer  or  DOUBLE POINTER .

More Related Content

PPTX
Pointer in C
PPTX
COM1407: Working with Pointers
PDF
PPT
Pointer in C
PPTX
Pointers
PPT
Pointers+(2)
PPTX
Pointer in c
PPTX
C Programming Unit-4
Pointer in C
COM1407: Working with Pointers
Pointer in C
Pointers
Pointers+(2)
Pointer in c
C Programming Unit-4

What's hot (20)

PPSX
C programming pointer
PPT
SPC Unit 3
PDF
9. pointer, pointer & function
PPTX
Pointer in c
PPT
PPTX
Pointers in C Language
PPTX
CSE240 Macros and Preprocessing
PPT
Functions and pointers_unit_4
PPT
detailed information about Pointers in c language
PDF
10. array & pointer
PPT
pointers
PDF
Data structure week 3
PPTX
Decision making and branching
PPTX
Expressions using operator in c
PPT
Pointers in C
PPTX
Function Pointer
PDF
C Pointers
PPTX
Pointers in c++
PDF
Module 02 Pointers in C
C programming pointer
SPC Unit 3
9. pointer, pointer & function
Pointer in c
Pointers in C Language
CSE240 Macros and Preprocessing
Functions and pointers_unit_4
detailed information about Pointers in c language
10. array & pointer
pointers
Data structure week 3
Decision making and branching
Expressions using operator in c
Pointers in C
Function Pointer
C Pointers
Pointers in c++
Module 02 Pointers in C
Ad

Viewers also liked (8)

DOCX
Projectrapport
DOC
Lan texto
PDF
Social Networking in the Business World: A Strategic Approach
PDF
Blog profissional
PPTX
Medical Terminology
PDF
Tesj008
PPTX
Marketing
PPTX
Donald Clark Citizens And Netizens Weapons Of Mass Collaboration
Projectrapport
Lan texto
Social Networking in the Business World: A Strategic Approach
Blog profissional
Medical Terminology
Tesj008
Marketing
Donald Clark Citizens And Netizens Weapons Of Mass Collaboration
Ad

Similar to Ch 7-pointers (20)

PDF
C pointers and references
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PDF
[ITP - Lecture 13] Introduction to Pointers
PPTX
pointers.pptx
PPTX
Unit-I Pointer Data structure.pptx
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPTX
pointers.pptx
PPT
iit c prog.ppt
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPTX
Chapter5.pptx
PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
PDF
VIT351 Software Development VI Unit3
PPTX
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PDF
Chapter 13.1.8
PPTX
Ponters
PPT
Unit 6 pointers
PDF
PSPC--UNIT-5.pdf
PDF
Pointers are one of the core components of the C programming language.
PDF
Lk module5 pointers
C pointers and references
UNIT 4 POINTERS.pptx pointers pptx for basic c language
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
[ITP - Lecture 13] Introduction to Pointers
pointers.pptx
Unit-I Pointer Data structure.pptx
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
pointers.pptx
iit c prog.ppt
Algoritmos e Estruturas de Dados - Pointers
Chapter5.pptx
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
VIT351 Software Development VI Unit3
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Chapter 13.1.8
Ponters
Unit 6 pointers
PSPC--UNIT-5.pdf
Pointers are one of the core components of the C programming language.
Lk module5 pointers

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
A Presentation on Artificial Intelligence
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
A Presentation on Touch Screen Technology
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Tartificialntelligence_presentation.pptx
Approach and Philosophy of On baking technology
Hindi spoken digit analysis for native and non-native speakers
Building Integrated photovoltaic BIPV_UPV.pdf
Zenith AI: Advanced Artificial Intelligence
A Presentation on Artificial Intelligence
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
DP Operators-handbook-extract for the Mautical Institute
A Presentation on Touch Screen Technology
Web App vs Mobile App What Should You Build First.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
A comparative study of natural language inference in Swahili using monolingua...
OMC Textile Division Presentation 2021.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
TLE Review Electricity (Electricity).pptx
1. Introduction to Computer Programming.pptx
Programs and apps: productivity, graphics, security and other tools
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Ch 7-pointers

  • 2. Pointers A pointer is a variable that holds a memory address. This address is the location of another object in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second. Basically, a pointer contains an address of another variable. Pointers provide an indirect means of accessing or retrieving the data from memory .
  • 3. Pointers Variable in memory 1000 1001 1002 1003 1004 1005 1006 103 Memory address
  • 4. The ‘&’ and ‘*’ Consider the declaration int ab=3; This declaration tells the compiler Reserve space in memory to hold the integer value. Associate the name ab with this memory location. Store the value 3 at this location.
  • 5. The ‘&’ and ‘*’ 3 ab 1000 Location name Value at Location Address
  • 6. Declaring pointer variable In C every variable must be declared for its data type. Since pointer variables contain addresses that belong to a separate data type they must be declared as pointers. The declaration of pointer variable takes following form: data_type *variable_name; For example:- int *q; here q is pointer variable that can hold the address
  • 7. The ‘&’ and ‘*’ ‘ &’  Address of operator ‘ *’  Value at address operator. Also called the Indirection Operator. ‘ &a’  Returns the address of variable a. ‘ *a’  Returns the value stored in a particular address.
  • 8. An Example:Pointers main() { int j=5; printf(“Address of j=%d\n”,&j); printf(“Value of j=%d\n”,j); printf(“Value of j=%d\n”,*(&j)); } Output: Address of j = 1000 Value of j = 5 Value of j = 5
  • 9. #include <stdio.h> int main() { int a = 10, b = 2; int *p1, *p2; p1 = &a; p2 = &b; printf(&quot;%u %u \n&quot;, p1, p2); printf(&quot;%d %d \n&quot;, *p1, *p2); return 0; } A program that prints out pointers and the values they point to:
  • 10. Write a program that computes the area and perimeter of a rectangle: #include <stdio.h> void rectangle(int a, int b, int * area, int * perim); int main() { int x, y; int area, perim; printf(&quot;Enter two values separated by space: &quot; ); scanf(&quot;%d %d&quot;, &x, &y); rectangle(x, y, &area, &perim); printf(&quot;Area is %d Perimeter is %d\n&quot;, area, perim); return 0; } void rectangle(int a,int b,int * area,int * perim) { *area = a * b; *perim = 2 * (a + b); }
  • 11. Pointer Expressions Expressions involving pointers can be classified as follows: Pointer Assignments Pointer Arithmetic Pointer Comparison Pointer Conversion
  • 12. Pointer Assignments Consider the following example Here i’s value is 5 and j’s value is i’s address. i 5 1000 1000 2000 j
  • 13. Pointer Assignments Since, the variable j is containing an address, it is declared as int *j; This declaration tells the compiler that j will be used to store the address of an integer value – i.e j points to an integer.
  • 14. An Example:Pointer Assignments main() { int j=3; int *k; k = &j; printf(“Address of j=%d”,&j); printf(“Address of j=%d”,k); printf(“Address of j=%d”,&k); printf(“Value of k=%d”,k); printf(“Value of j=%d”,j); printf(“Value of j=%d”,*(&j)); printf(“Value of j=%d”,*j); } Output: Address of j = 1000 Address of j = 1000 Address of k = 2000 Value of k = 1000 Value of j = 3 Value of j = 3 Value of j = error
  • 15. Pointer Conversions One type of pointer can be converted to another type of pointer. Consider the following example: main() { double x = 100.1,y; int *p; /*The next statement causes p to point to double*/ p = (int*)&x; y = *p; printf(“The value of x is: %f”,y); }
  • 16. Pointers v/s Arrays Consider the following program main() { static char arr[10]=“Embedded”; char *s=“Embedded”; printf(“%s\n”,arr); printf(“%s\n”,s); } Output: Embedded Embedded
  • 17. Pointers v/s Arrays Consider the following program main() { static char arr[10]=“Embedded”; char *s=“Embedded”; s++; printf(“%s\n”,arr); printf(“%s\n”,s); } Output : Embedded mbedded
  • 18. Analysis: Pointers v/s Arrays s++ increments s such that it starts pointing to ‘m’ of Embedded and hence would print mbedded as output. arr++ would give an error message because the information known to us is the base address and arr++ is attempting to change this base address.
  • 19. Arrays of Pointers As there are an array of ints, floats, similarly there can be an array of pointers. An array of pointers represent a collection of addresses.
  • 20. An Example: Arrays of Pointers main() { int *arr[4]; int i=5,j=10,k=15,l=20,m; arr[0]=&i; arr[1]=&j; arr[2]=&k; arr[3]=&l; for(m=0;m<=3;m++) printf(“%d”,*(arr[m])); }
  • 21. Arrays of Pointers 1000 2500 2000 1500 i 5 10 j 20 15 l k 2500 2000 1500 1000 arr[0] arr[3] arr[2] arr[1] 1200 2700 2200 1700
  • 22. Pointers To Pointers Pointer as we know is a variable which contains an address of another variable. A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER .
  • 23. Double Pointers 3 i 1000 1000 2000 j 2000 3000 k
  • 24. Double Pointer main() { int i=3; int *j; int **k; j=&i; k=&j; printf(“Address of i is %d%d%d\n”,&i,j,*k); printf(“Address of j is %d%d\n”,&j,k); printf(“Address of k is %d\n”,&k); printf(“Value of i is %d%d%d%d\n”,i,*(&i),*j,**k); printf(“Value of j is %d\n”,j); printf(“Value of k is %d\n”,k); }
  • 25. Double Pointer OUTPUT: Address of i is 1000 1000 1000 Address of j is 2000 2000 Address of k is 3000 Value of i is 3 3 3 3 Value of j is 1000 Value of k is 2000
  • 26. Summary A pointer is a variable that holds a memory address. Basically, a pointer contains an address of another variable. Pointers provide an indirect means of accessing or retrieving the data from memory. Arithmetic operations such as addition, multiplication and division on two pointers are not allowed. A pointer variable cannot be multiplied/divided by a constant or a variable. A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER .