Simulating Reference Parameters in C

Slides:



Advertisements
Similar presentations
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Advertisements

1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Introduction to C Programming CE
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
UNIT 14 Functions with Pointer Parameters.
1 CS161 Introduction to Computer Science Topic #10.
Object-Oriented Programming in C++
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Copyright © – Curt Hill Pointers A Light Introduction.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Functions Functions, locals, parameters, and separate compilation.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
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.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
CPSC 221 Call by value, pointer and reference in C++ Page 1 Hassan Khosravi January – April 2015 CPSC 221 Basic Algorithms and Data Structures Call by.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Advanced Programming in C Passing Parameter to Function.
Lecture 5 Pointers 1. Variable, memory location, address, value
Functions (Methods) Pascal C, C++ Java Scripting Languages
User-Written Functions
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
Pointers and Pass By Reference
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
CS1010 Programming Methodology
Pointers and Pointer-Based Strings
Pointers.
Programming Fundamentals Lecture #7 Functions
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Templates.
INC 161 , CPE 100 Computer Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Pointers and References
Functions Inputs Output
Anatomy of a Function Part 2
Object Oriented Programming COP3330 / CGS5409
Alternate Version of STARTING OUT WITH C++ 4th Edition
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Pointers Call-by-Reference CSCI 230
Parameter Passing in Java
Programming in C Pointer Basics.
CS2011 Introduction to Programming I Methods (II)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Why did the programmer quit his job?
Pointers and References
Pointers and Pointer-Based Strings
Pointers & Functions.
ETE 132 Lecture 6 By Munirul Haque.
Lecture 6: References and linked nodes reading: 16.1
Arrays and Pointers CSE 2031 Fall May 2019.
Pointers and References
pointer-to-pointer (double pointer)
Arrays and Pointers CSE 2031 Fall July 2019.
C Parameter Passing.
Presentation transcript:

Simulating Reference Parameters in C Suppose we want to implement a swap function that takes two variables and interchanges their values. For example, if 7 is stored in a and 5 is stored in b, then after the call, swap(a, b), we want 5 to be stored in a and 7 to be stored in b. We might be tempted to write the following code: #include <stdio.h> void swap(int x, int y); int main() { int a = 7; int b = 5; printf("\nBefore swap: a = %d \t b = %d\n", a, b); swap(a, b); printf("\nAfter swap: a = %d \t b = %d\n", a, b); return 0; }

Simulating Reference Parameters in C void swap(int x, int y) { int temp = x; x = y; y = temp; } If you execute this program, you will see that it does not produce the desired result. Why? a and b are value parameters; consequently swap gets a copy of the values passed to it and stores these values in its own local variables x and y. Output: Before swap: a = 7 b = 5 After swap: a = 7 b = 5

Simulating Reference Parameters in C main swap (before execution) main swap (after execution) After swap returns, all variables associated with swap go away. main Since main cannot access the local variables of swap, main does not see the results. a 7 b 5 7 x ? temp 5 y x 5 7 temp y

Simulating Reference Parameters in C What is needed is a way to pass parameters by reference instead of passing by value. To correct the problem, we use pointers. C does not have reference parameters. #include <stdio.h> void swap(int *x, int *y); // note the use of pointers int main() { int a = 7; int b = 5; printf("\nBefore swap: a = %d \t b = %d\n", a, b); swap(&a, &b); // we must pass the address of a and the address of b printf("\nAfter swap: a = %d \t b = %d\n", a, b); return 0; }

Simulating Reference Parameters in C void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } Output: Before swap: a = 7 b = 5 After swap: a = 5 b = 7

Simulating Reference Parameters in C main swap (before execution) main swap (after execution) Now, when swap terminates, the actual parameters in main have been modified. a 7 b 5 x ? temp y a 5 b 7 x 7 temp y