Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Pointers and Memory Allocation -L. Grewe. Objectives Why and What are Pointers Create Pointers in C++ Memory Allocation Defined Memory Allocation/Deallocation.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Pointers Ethan Cerami Fundamentals of Computer New York University.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
1 ICS103 Programming in C Lecture 10: Functions II.
Pointers Applications
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
UNIT 8 Pointers.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
CS1010 Programming Methodology
User-Written Functions
Pointers.
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
POINTERS.
Pointers.
Tejalal Choudhary “C Programming from Scratch” Pointers
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Pointers and Pointer-Based Strings
Introduction to Pointers
Presentation transcript:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 2Winter Quarter 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.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 3Winter Quarter 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

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 4Winter Quarter 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.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 5Winter Quarter POINTERS Consider the statements: #include 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 */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 6Winter Quarter POINTERS /* Then consider the statements: */ aptr = &a ; bptr = &b ; fptr2 = fopen ( "my_out_file.dat", "w" ) ; fptr1 = fopen ( "my_in_file.dat", "r" ) ; if ( fptr1 != NULL ) { fscanf ( fptr1, "%d%f", aptr, bptr ) ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 7Winter Quarter POINTERS fprintf ( fptr2, "%d %d\n", aptr, bptr ) ; fprintf ( fptr2, "%d %f\n", *aptr, *bptr ) ; fprintf ( fptr2, "%d %f\n", a, b ) ; fprintf ( fptr2, "%d %d\n", &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?

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 8Winter Quarter Use of & and * When is & used? When is * used? & -- "address operator" which gives or produces the memory address of a data variable * -- "dereferencing operator" which provides the contents in the memory location specified by a pointer

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 9Winter Quarter POINTERS aptr = &a ; bptr = &b ; fptr2 = fopen ( "my_out.dat", "w" ) ; fptr1 = fopen ( "my_in.dat", "r" ) ; if ( fptr1 != NULL ) { fscanf (fptr1, "%d%f", aptr, bptr); fprintf (fptr2, "%d %d\n", aptr, bptr ) ; fprintf (fptr2, "%d %f\n", *aptr, *bptr ) ; fprintf (fptr2, "%d %f\n", a, b ) ; fprintf (fptr2, "%d %d\n", &a, &b ) ; return 0 ; } /* input file */ /* output file */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 10Winter Quarter 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 "call by value".

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 11Winter Quarter 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 "call by reference" since we are referencing the variables. The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 12Winter Quarter Pointers with Functions (example) #include void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; printf("a=%d b=%d\n",a,b) ; swap (&a, &b) ; printf("a=%d b=%d\n",a,b) ; return 0 ; } void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf ("a=%d b=%d\n", *a, *b); } Results: a=5 b=6 a=6 b=5

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 13Winter Quarter 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.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 14Winter Quarter 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 , then the statement: valptr = valptr + 2; would change valptr to

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 15Winter Quarter Using the C Language Special Keyword sizeof This keyword can be used to determine the number of bytes in a data type, a variable, or an array Example: double array [10]; sizeof (double); /* Returns the value 8 */ sizeof (array); /* Returns the value 80 */ sizeof(array)/sizeof(double); /* Returns 10 */