SlideShare a Scribd company logo
 
Pointers Pointers are variables that contain  memory   addresses  as their values. A variable name  directly  references a value. A pointer  indirectly  references a value.  Referencing a value through a pointer is called  indirection . A pointer variable must be declared before it can be used.
Concept of Address and Pointers Memory can be conceptualized as a linear set of data locations. Variables reference the contents of a locations Pointers have a value of the address of a given location Contents1 Contents11 Contents16 ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 * * * ADDR11 * * ADDR16
POINTERS Examples of pointer declarations: FILE  *fptr; int  *a; float *b; char *c; The  asterisk , when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but  NOT  the name of the variable pointed to.
POINTERS Consider the statements: #include <stdio.h> int main ( ) { FILE  *fptr1 , *fptr2 ; /* Declare two file pointers */ int *aptr ; /* Declare a pointer to an int */ float *bptr ; /* Declare a pointer to a float */ int a ; /* Declare an int variable */ float b ; /* Declare a float variable */
POINTERS /*  Then consider the statements:  */ aptr = &a ; bptr = &b ; fptr2 = fopen ( &quot;my_out_file.dat&quot; , &quot;w&quot; ) ;  fptr1 = fopen ( &quot;my_in_file.dat&quot; , &quot;r&quot; ) ; if ( fptr1 != NULL ) {   fscanf ( fptr1, &quot;%d%f&quot; , aptr , bptr ) ;
POINTERS   fprintf ( fptr2, &quot;%d %d\n&quot; , aptr , bptr ) ;   fprintf ( fptr2, &quot;%d %f\n&quot; , *aptr , *bptr ) ;   fprintf ( fptr2, &quot;%d %f\n&quot; , a , b ) ; fprintf ( fptr2, &quot;%d %d\n&quot; , &a , &b ) ;   return 0 ; } Assuming that the above is part of a program that runs without error and the the input file does open, what would be printed to the file  By the first fprintf?  By the second fprintf? By the third fprintf?  By the fourth fprintf?
Use of & and * When is & used? When is * used? & -- &quot;address operator&quot; which gives or produces the memory address of a data variable * -- &quot;dereferencing operator&quot; which provides the contents in the memory location specified by a pointer
POINTERS aptr = &a ; bptr = &b ; fptr2 = fopen ( &quot;my_out.dat&quot; , &quot;w&quot; ) ; fptr1 = fopen ( &quot;my_in.dat&quot; , &quot;r&quot; ) ; if ( fptr1 != NULL ) { fscanf (fptr1, &quot;%d%f&quot;,  aptr,  bptr); fprintf (fptr2, &quot;%d %d\n&quot;,  aptr,  bptr ) ; fprintf (fptr2, &quot;%d %f\n&quot;,  *aptr,  *bptr ) ; fprintf (fptr2, &quot;%d %f\n&quot;,  a ,  b ) ; fprintf (fptr2, &quot;%d %d\n&quot;,  &a ,  &b ) ; return 0 ; } } /* input file */ 5  6.75 /* output file */ 1659178974 1659178976 5 6.750000 5 6.750000 1659178974 1659178976
Pointers and Functions Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as &quot;call by value&quot;.
Pointers and Functions If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine.  This is known as &quot;call by reference&quot; since we are  referencing  the variables. The following shows the swap function modified from a &quot;call by value&quot; to a &quot;call by reference&quot;.  Note that the values are now actually swapped when the control is returned to main function.
Pointers with Functions (example) #include <stdio.h> void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; printf(&quot;a=%d b=%d\n&quot;,a,b) ; swap (&a, &b) ; printf(&quot;a=%d b=%d\n&quot;,a,b) ; return 0 ; }   void swap( int *a, int *b ) { int temp; temp= *a;  *a= *b;  *b = temp ; printf (&quot;a=%d  b=%d\n&quot;, *a, *b); } Results: a=5  b=6 a=6  b=5 a=6  b=5
Arithmetic and Logical Operations on Pointers A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
Arithmetic Operations on Pointers When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to. For example, if the pointer  valptr  contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr  + 2; would change  valptr   to 234567886

More Related Content

PPTX
Pointer in c
PPT
Pointers C programming
PPTX
Pointer in c program
PPTX
COM1407: Working with Pointers
PPTX
Pointers In C
PPTX
C Programming: Structure and Union
PPTX
Pointers in c - Mohammad Salman
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointer in c
Pointers C programming
Pointer in c program
COM1407: Working with Pointers
Pointers In C
C Programming: Structure and Union
Pointers in c - Mohammad Salman
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...

What's hot (20)

PPT
pointers
PPTX
Passing an Array to a Function (ICT Programming)
PPT
Pointers in C
PDF
Pointers in C
PPT
File handling in c
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPTX
Operators in python
DOC
String in c
PPTX
C-Programming Function pointers.pptx
PPTX
pointers.pptx
PPT
Mesics lecture files in 'c'
PPTX
Pointer in C++
PPTX
Python Data-Types
PDF
Unit II chapter 4 Loops in C
PPTX
self_refrential_structures.pptx
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
Linked list and its operations - Traversal
PPTX
Structure & union
pointers
Passing an Array to a Function (ICT Programming)
Pointers in C
Pointers in C
File handling in c
CONDITIONAL STATEMENT IN C LANGUAGE
Operators in python
String in c
C-Programming Function pointers.pptx
pointers.pptx
Mesics lecture files in 'c'
Pointer in C++
Python Data-Types
Unit II chapter 4 Loops in C
self_refrential_structures.pptx
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
Linked list and its operations - Traversal
Structure & union
Ad

Viewers also liked (20)

PDF
Pointers
PDF
C Pointers
PPT
Unit 6 pointers
PPTX
Pointers in C Programming
PPT
Pointers in c
PPTX
C programming - Pointers
PPTX
C++ Pointers
PPTX
PPTX
Pointers in c++
PDF
Pointer in c++ part1
PPTX
Pointers in c++
PPTX
CSE240 Pointers
PPT
Pointers - DataStructures
PDF
Arrays in C++
PPT
Pointer in C
PPTX
intro to pointer C++
PPT
detailed information about Pointers in c language
PPTX
C programming - Pointer and DMA
PPT
C chap02
PPT
File handling-dutt
Pointers
C Pointers
Unit 6 pointers
Pointers in C Programming
Pointers in c
C programming - Pointers
C++ Pointers
Pointers in c++
Pointer in c++ part1
Pointers in c++
CSE240 Pointers
Pointers - DataStructures
Arrays in C++
Pointer in C
intro to pointer C++
detailed information about Pointers in c language
C programming - Pointer and DMA
C chap02
File handling-dutt
Ad

Similar to Ponters (20)

PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
PPT
iit c prog.ppt
PPTX
C programming
PPTX
Cplusplus
PPT
Falcon初印象
PPT
PPT
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
PPTX
Function Pointer
PPT
About Go
PPTX
pointers.pptx
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPT
c program.ppt
PPT
presentation_pointers_1444076066_140676 (1).ppt
PDF
9. pointer, pointer & function
PPTX
Pointer in C
PDF
4 operators, expressions &amp; statements
PPT
ch08.ppt
PPT
pointer, structure ,union and intro to file handling
PPT
Advanced pointers
PDF
Lk module5 pointers
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
iit c prog.ppt
C programming
Cplusplus
Falcon初印象
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
Function Pointer
About Go
pointers.pptx
UNIT 4 POINTERS.pptx pointers pptx for basic c language
c program.ppt
presentation_pointers_1444076066_140676 (1).ppt
9. pointer, pointer & function
Pointer in C
4 operators, expressions &amp; statements
ch08.ppt
pointer, structure ,union and intro to file handling
Advanced pointers
Lk module5 pointers

More from Mukund Trivedi (20)

PPTX
System development life cycle (sdlc)
PPTX
Process of design
PPT
New file and form 2
PPT
File organisation
PPTX
Evaluation
PPTX
Database
PPTX
Case tools
PPTX
Evaluation
PPTX
Dfd final
DOCX
C++ file
PPT
Ff40fnatural resources (1)
PPT
Ff40fnatural resources
PPT
F58fbnatural resources 2 (1)
PPT
F58fbnatural resources 2
PPT
F6dc1 session6 c++
DOC
Ee2fbunit 7
PPT
E212d9a797dbms chapter3 b.sc2 (2)
PPT
E212d9a797dbms chapter3 b.sc2 (1)
PPT
E212d9a797dbms chapter3 b.sc2
System development life cycle (sdlc)
Process of design
New file and form 2
File organisation
Evaluation
Database
Case tools
Evaluation
Dfd final
C++ file
Ff40fnatural resources (1)
Ff40fnatural resources
F58fbnatural resources 2 (1)
F58fbnatural resources 2
F6dc1 session6 c++
Ee2fbunit 7
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
project resource management chapter-09.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Approach and Philosophy of On baking technology
PDF
August Patch Tuesday
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Tartificialntelligence_presentation.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
A Presentation on Touch Screen Technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
1. Introduction to Computer Programming.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Assigned Numbers - 2025 - Bluetooth® Document
project resource management chapter-09.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Approach and Philosophy of On baking technology
August Patch Tuesday
NewMind AI Weekly Chronicles - August'25-Week II
cloud_computing_Infrastucture_as_cloud_p
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A novel scalable deep ensemble learning framework for big data classification...
Tartificialntelligence_presentation.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
A Presentation on Touch Screen Technology
SOPHOS-XG Firewall Administrator PPT.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MIND Revenue Release Quarter 2 2025 Press Release
1. Introduction to Computer Programming.pptx

Ponters

  • 1.  
  • 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection . A pointer variable must be declared before it can be used.
  • 3. Concept of Address and Pointers Memory can be conceptualized as a linear set of data locations. Variables reference the contents of a locations Pointers have a value of the address of a given location Contents1 Contents11 Contents16 ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 * * * ADDR11 * * ADDR16
  • 4. POINTERS Examples of pointer declarations: FILE *fptr; int *a; float *b; char *c; The asterisk , when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.
  • 5. POINTERS Consider the statements: #include <stdio.h> int main ( ) { FILE *fptr1 , *fptr2 ; /* Declare two file pointers */ int *aptr ; /* Declare a pointer to an int */ float *bptr ; /* Declare a pointer to a float */ int a ; /* Declare an int variable */ float b ; /* Declare a float variable */
  • 6. POINTERS /* Then consider the statements: */ aptr = &a ; bptr = &b ; fptr2 = fopen ( &quot;my_out_file.dat&quot; , &quot;w&quot; ) ; fptr1 = fopen ( &quot;my_in_file.dat&quot; , &quot;r&quot; ) ; if ( fptr1 != NULL ) { fscanf ( fptr1, &quot;%d%f&quot; , aptr , bptr ) ;
  • 7. POINTERS fprintf ( fptr2, &quot;%d %d\n&quot; , aptr , bptr ) ; fprintf ( fptr2, &quot;%d %f\n&quot; , *aptr , *bptr ) ; fprintf ( fptr2, &quot;%d %f\n&quot; , a , b ) ; fprintf ( fptr2, &quot;%d %d\n&quot; , &a , &b ) ; return 0 ; } Assuming that the above is part of a program that runs without error and the the input file does open, what would be printed to the file By the first fprintf? By the second fprintf? By the third fprintf? By the fourth fprintf?
  • 8. Use of & and * When is & used? When is * used? & -- &quot;address operator&quot; which gives or produces the memory address of a data variable * -- &quot;dereferencing operator&quot; which provides the contents in the memory location specified by a pointer
  • 9. POINTERS aptr = &a ; bptr = &b ; fptr2 = fopen ( &quot;my_out.dat&quot; , &quot;w&quot; ) ; fptr1 = fopen ( &quot;my_in.dat&quot; , &quot;r&quot; ) ; if ( fptr1 != NULL ) { fscanf (fptr1, &quot;%d%f&quot;, aptr, bptr); fprintf (fptr2, &quot;%d %d\n&quot;, aptr, bptr ) ; fprintf (fptr2, &quot;%d %f\n&quot;, *aptr, *bptr ) ; fprintf (fptr2, &quot;%d %f\n&quot;, a , b ) ; fprintf (fptr2, &quot;%d %d\n&quot;, &a , &b ) ; return 0 ; } } /* input file */ 5 6.75 /* output file */ 1659178974 1659178976 5 6.750000 5 6.750000 1659178974 1659178976
  • 10. Pointers and Functions Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as &quot;call by value&quot;.
  • 11. Pointers and Functions If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as &quot;call by reference&quot; since we are referencing the variables. The following shows the swap function modified from a &quot;call by value&quot; to a &quot;call by reference&quot;. Note that the values are now actually swapped when the control is returned to main function.
  • 12. Pointers with Functions (example) #include <stdio.h> void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; printf(&quot;a=%d b=%d\n&quot;,a,b) ; swap (&a, &b) ; printf(&quot;a=%d b=%d\n&quot;,a,b) ; return 0 ; } void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf (&quot;a=%d b=%d\n&quot;, *a, *b); } Results: a=5 b=6 a=6 b=5 a=6 b=5
  • 13. Arithmetic and Logical Operations on Pointers A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
  • 14. Arithmetic Operations on Pointers When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to. For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr + 2; would change valptr to 234567886

Editor's Notes

  • #3: We have already used pointer without knowing it when they declare FILE variables, which basically point to the address where a file stream is located. Pointers are variables that contain instead of data, the memory address where the data exists as their values. It can be said that a regular variable name directly references the value in that variable. A pointer, on the other hand, indirectly references a value. Referencing through a pointer is called indirection. Like any other variable, a pointer must be declared before it can be used.
  • #4: A computer has two basic types of busses to the memory: an address and data bus. Thus far we have been concerned mainly with the data we store but not its storage location. Pointers allow us to also use the address in which our data is stored in various ways. This conceptualization of memory can be likened to a street with various address where mail is delivered. The mailman delivers the correct data (letters) to the correct address written on each piece of mail.
  • #5: Pointers can be declared with regular variables in the same line, the asterisk (or lack of one) determines if the variable will contain an address or data. It is important to declare the correct type of pointer depending on the type of data it will point to. It is NOT possible to give a variable and pointer the same name. All variables (pointer or data) must still have a unique name. Pointers are declared much like variables, except an asterisk must be before each variable name. Pointers can be declared with regular variables in the same line, the asterisk (or lack of one) determines if the variable will contain an address or data. It is important to declare the correct type of pointer depending on the type of data it will point to. It is NOT possible to give a variable and pointer the same name. All variables (pointer or data) must still have a unique name.
  • #6: In this case the pointer and variable declarations are separated. They could have also been handled as such: FILE *fptr1, *fptr2; int *aptr, a; int *bptr, b; In these statements we are declaring pointers of multiple types, along with the familiar FILE pointer type. We also declare normal variables. In this case the pointer and variable declarations are separated. Pointers and variables of the same type could have been declared on the same line, separated by commas.
  • #7: Remember that including the ampersand (&amp;) before any variable’s name returns its location in memory (first introduced with scanf() and fscanf()). So to set a pointer to the location in memory of a variable we simply set the pointer equal to the variable with a preceding ampersand such as those shown. We can then use the fscanf with the pointers aptr and bptr instead of &amp;a and &amp;b since both return the same information (the memory location of variables a and b).
  • #8: Remember fprintf() returns the value stored in a variable (without the need of a preceding ampersand). 1 st fprintf: returns the values stored in aptr and bptr (the memory locations of a and b). (the addresses) 2 nd fprintf: returns the values stored in the memory locations stored in aptr and bptr (the actual values of a and b). (the data) – called dereferencing a pointer 3 rd fprintf: returns the values of a and b (the data) 4 th fprintf: returns the memory locations of a and b (the addresses) Notice the difference in the variables printed in each fprintf() function here. Can you determine which variable will print which values? 1 st fprintf: returns the values stored in aptr and bptr (the memory locations of a and b). (the addresses) 2 nd fprintf: returns the values stored in the memory locations stored in aptr and bptr (the actual values of a and b). (the data) – called dereferencing a pointer 3 rd fprintf: returns the values of a and b (the data) 4 th fprintf: returns the memory locations of a and b (the addresses)
  • #9: Use the &amp; when you wish to know the address of a variable. Use the * when you wish to return the value stored in the variable as an address , then look in that address and return its data With the introduction of pointers, we now have the &amp; and the * being used for various operations. The &amp; is used when you wish to know the address of a variable. This is known as the address operator. The *, or dereferencing operator, provides the contents of the memory location specified by a pointer. This operation takes the value stored in the pointer as an address, looks in that address and returns the data stored there.
  • #10: The output of this program is simulated with two values from an input file and output given in the right column. The numbers representing addresses in this example would change if each student were to execute the program (each student would receive their own block of memory to use) and may also change during each execution of the program depending on if the operating system keeps the program cached after each use. The output of this program is simulated with two values from an input file and output given in the right column. As expected, pointers and variables with address operators return the addresses of the a and b variables. Also the dereferenced pointers and variables themselves return the values of the variables. The numbers representing addresses in this example would change if you were to execute the program and may also change during each execution of the program depending on if the operating system keeps the program cached after each use
  • #11: Looking back as the swap function problem, the variable scope problem can now be solved by passing pointers, or the original location of variables, instead of just the values. The next slide explains more. So now we can see how pointers can be used to pass addresses of variables around. They can also be used to pass addresses of variables to functions, thus allowing the function to alter the values of the original variables. Looking back at the swap function, you may remember it received copies of the variables, since they were called by value.
  • #12: Call by reference is now explained. It simply involves passing the location in memory of the original variables to any supporting function. If instead of passing the value of variables to the function, we now pass the addresses so that the function can change the values stored in the calling routine. The is known as “called by reference” since we are referencing the variables. The swap function is shown again modified to call by reference instead of call by value. This will now actually swap the values in the function, and will return to the main with actual swapped values.
  • #13: Here is a modified swap program. Notice the swap function now requires pointer variables as input (both in the function and function prototype), and that all operations are done by dereferencing the pointers within the swap function to get the values and move them around with the aid of the local “temp” variable. In the main() function the addresses of the a and b variable are passed to the swap() function (given by &amp;a and &amp;b). The results are now what would be expected of a swap function.
  • #14: Most logical operations can be used on pointers as with regular variables, but the programmer must be careful of the results since each program doesn’t have free reign of the entire computer’s memory space. Errors which attempt to use memory not assigned to a certain program will cause what will be seen as a segmentation fault or bus error in UNIX, or Windows’ extreme, more deadly blue screen of death. Pointers can be incremented and decremented. Integers can be added or subtracted from a pointer, and pointers variable may be subtracted from one another. Pointers can also be used in comparisons, but usually only in a comparison to NULL as been shown when testing file pointers for a successful file open.
  • #15: Memory arithmetic is slightly more complicated than standard algebra. Each byte of memory has its own unique address. Remember that different data types use multiple bytes of memory thus span multiple addresses. The UNIX data sizes are reviewed: int: 32 bit – 4 bytes float: 32 bit – 4 bytes double: 64 bit – 8 bytes char: 8 bit – 1 byte So addition of y to a pointer will actually be a multiple of y*x where x is the size of the data type in bytes. In the example shown here, the pointer valptr contains the address of a double precision variable and the address is shown. Each double precision number is 8 bytes. The addition of 2 to the pointer is then 2*8, or 16. Thus we can see the addition of 2 to the pointer adds 16 to the address.