ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Programming and Data Structure
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:
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Introduction to C Programming CE
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
CSE 220 – C Programming Pointers.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
Student Book An Introduction
ECE 103 Engineering Programming Chapter 32 Array Parameters
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Homework Starting K&R Chapter 5 Good tutorial on pointers
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Pointers and Pointer-Based Strings
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Presentation transcript:

ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

Syllabus Value Parameters Reference Parameters Passing 1-D Arrays to Functions Revisited Returning a Pointer from a Function

2 Pass by Value and Pass by Reference Generally, C parameters are passed by value With the exception of array parameters, which are passed by reference C++ also introduces explicit reference parameter passing via the & specifier; not part of C Consequences of "pass by value":  The function receives copies of the arguments, which are stored in local parameter variables  The function can change the parameter values, but it cannot change the values of the original arguments

3 Sometimes, the value of a passed argument needs to be changeable from inside the function. Example:  To return more than a single value from a function.  To swap the values of two arguments. How can this be done if the C language only supports pass by value? Answer: Pass the memory address of a variable instead of the value contained in that variable!

4 By using pointers as function parameters, the original arguments can be modified indirectly. To declare a parameter that is a pointer: return_dtype fun_name (…, dtype * param_name, …) { /* Function body */ } This allows C to simulate pass by reference.

5 While arguments can be changed via pointers, it is not required to do so. Passing just the pointer to a large data block may improve space and time efficiency. Example: You pass a data structure 1 MB in size to a function. If passed as a normal argument, then a duplicate 1 megabyte block has to be created. If a pointer to the data block is passed instead, then just a few bytes (the address) are transferred.

6 If a pointer is passed to a function, but the argument must not be changed, then use the const modifier in the parameter declaration. Example: void fun1 (char *s) { /* Can change what s points to */ } void fun2 (const char *s) { /* Cannot change what s points to */ }

7 Example: Function definition: void myfun (int a, float *b, float *c, float **d, float **e) { … } ParameterDatatype of parameterExpected argument a intinteger value b pointer to floatmemory address c pointer to floatmemory address d pointer to pointer to floataddress of a variable that contains another address e pointer to pointer to floataddress of a variable that contains another address

8 Invoking the function: void myfun (int a, float *b, float *c, float **d, float **e); int x = 3;/* Simple variable */ float y = 1.0;/* Simple variable */ float *p = &y;/* p points to y */ float **q = &p;/* q points to p */ myfun(x, &y, p, &p, q); /* Invoke */ Matching arguments to parameters: x  int a &y  float *b p  float *c &p  float **d q  float **e

9 Example: /* Non-pointer parameters */ #include #define PI float circ (float R) { return 2*PI* R; } int main (void) { float radius = 0.75; float C; C = circ(radius); printf("%f\n", C); return 0; } /* Pointer parameters */ #include #define PI float circ (float *R) { return 2*PI* (*R); } int main (void) { float radius = 0.75; float C; C = circ(&radius); printf("%f\n", C); return 0; } Expects a value Retrieve value Pass value to functionExpects an address Retrieve value stored at address Pass address to function

10 Example: /* Non-pointer parameters */ #include double mag (double x, double y) { return (sqrt(x*x + y*y)); } double angle (double x, double y) { return (atan2(y,x)); } int main (void) { double m, a; double u = 1.0, v = 1.0; m = mag(u, v); a = angle(u, v); return 0; } /* Pointer parameters */ #include void mang (double x, double y, double *m, double *a) { *m = sqrt(x*x + y*y); *a = atan2(y,x); } int main (void) { double m, a; double u = 1.0, v = 1.0; mang(u, v, &m, &a); return 0; }

11 Example: /* Try to swap values of a and b */ #include /* This will not work */ void swap (int x, int y) { int temp; temp = x; x = y; y = temp; } int main (void) { int a = 1, b = 3; printf("Pre : a=%d b=%d\n", a, b); swap(a, b); printf("Post: a=%d b=%d\n", a, b); return 0; } Actual Output: Pre : a=1 b=3 Post: a=1 b=3

12 Example: /* Try to swap values of a and b */ #include /* This will work */ void swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main (void) { int a = 1, b = 3; printf("Pre : a=%d b=%d\n", a, b); swap(&a, &b); printf("Post: a=%d b=%d\n", a, b); return 0; } Actual Output: Pre : a=1 b=3 Post: a=3 b=1

13 Passing 1-D Arrays to Functions Revisited If a function expects a 1-D array argument, the corresponding parameter can be declared using either array notation or pointer notation. Example: void fun1 (double A[]) { … } void fun2 (double *A) { … } Both forms work if the argument is a 1-D array. double v[5]; fun1(v); /* Remember that v by itself */ fun2(v); /* is the address of v[0]. */

14 Returning a Pointer from a Function A function can return a value. It can also return a pointer to a value. What does it mean to return a pointer? Answer: A memory address is returned. Declaration: return_datatype * function_name (…) { /* Function body */ }

15 Example: #include /* Capitalizes a string */ char * capstr (char * s) { char *p = s; while (*p) *p++ = toupper((int)*p); return s; /* Address */ } int main (void) { char s[] = "Hello"; printf("Pre : %s\n", s); printf("Post1: %s\n", capstr(s)); printf("Post2: %s\n", s); return 0; } Actual Output: Pre : Hello Post1: HELLO Post2: HELLO

16 Example: #include int * fun1 (int *s) { return s; } int * fun2 (int s) { return &s; } int main (void) { int x; printf("%p\n", &x); printf("%p\n", fun1(&x)); printf("%p\n", fun2(x)); return 0; } Actual Output: 0x22cd64 ← &x 0x22cd64 ← fun1(&x) 0x22cd40 ← fun2(x) fun1 Notes: The parameter *s is local to fun1. It contains the address that was passed to it (i.e., &x ), which is also the address that will be returned. fun2 Notes: The parameter s is local to fun2. When the address &s is returned, it is the address of the local parameter s, and not of the argument that was passed to fun2. When a function call is done, local variables defined in the function will no longer exist, and the memory addresses of those local variables become invalid.