Pointers, Arrays and Parameters

Similar documents
Programming Studio #9 ECE 190

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CSE 333 Midterm Exam 7/29/13

CMSC202 Computer Science II for Majors

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Dynamic Memory Allocation

Functions BCA-105. Few Facts About Functions:

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

REFERENCES, POINTERS AND STRUCTS

Object-Oriented Programming

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 04 Introduction to pointers

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Object-Oriented Principles and Practice / C++

Pointers (1A) Young Won Lim 3/5/18

by Pearson Education, Inc. All Rights Reserved.

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 2/10/18

CA31-1K DIS. Pointers. TA: You Lu

Object Model Comparisons

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Instructions for Crossword Assignment CS130

Relationship of class to object

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CSE 333 Midterm Exam Sample Solution 7/29/13

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

5.6.1 The Special Variable this

Exceptions and Design

ENCM 501 Winter 2019 Assignment 9

Today's Topics. CISC 458 Winter J.R. Cordy

Intermediate Programming, Spring 2017*

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

DCS/100 Procedural Programming

Memory and Pointers written by Cathy Saxton

CS 251 Intermediate Programming Java Basics

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

For example, let s say we define an array of char of size six:

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

EL6483: Brief Overview of C Programming Language

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Text Input and Conditionals

Expressions and Casting

Ch. 12: Operator Overloading

Advanced Computer Programming

AP Computer Science Unit 1. Writing Programs Using BlueJ

Lab 3. Pointers Programming Lab (Using C) XU Silei

Introduction to C++ with content from

CSE 303 Midterm Exam

CP Lab 5: Functions, pointers, some arrays

CP Lab 5: Functions, pointers, some arrays

C Pointers 2013 Author Riko H i

pointers + memory double x; string a; int x; main overhead int y; main overhead

QUIZ Friends class Y;

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Chapter 1: Object-Oriented Programming Using C++

CS16 Exam #1 7/17/ Minutes 100 Points total

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Lecture 2: C Programm

What goes inside when you declare a variable?

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

4. Functions. March 10, 2010

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Week 4. This is CS50. Harvard University. Fall Anna Whitney

CSE 143 Lecture 12 Inheritance

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

SPIM Procedure Calls

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

1 Getting started with Processing

Linked lists Tutorial 5b

Advanced Computer Programming

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

C++ for Java Programmers

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Menus. You ll find MenuStrip listed in the Toolbox. Drag one to your form. Where it says Type Here, type Weather. Then you ll see this:

Pointers (1A) Young Won Lim 1/14/18

Chapter - 9 Variable Scope and Functions. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

C introduction: part 1

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Introduction to pthreads

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Transcription:

Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming language you are learning. To do so, we are going to write little programs which illustrate the relation between what we write and how it executes. Swapping two integer arrays, the basic way (works just fine) Here is the main function of a little program which is going to declare two arrays of integers (same size) and which is going to call a function to swap their respective contents. #define SIZE 3 void swap_arrays_1 ( int p1[], int p2[], int size); int main ( ) int a1[ SIZE ] = 0,0,0 ; int a2[ SIZE ] = 1,1,1 ; swap_arrays_1 ( a1, a2, SIZE ) ; void swap_arrays_1 ( int p1[], int p2[], int size) int i, tmp ; for ( i = 0 ; i < size ; i++ ) tmp = p1[i] ; p1[i] = p2[i] ; p2[i] = tmp ; Complete the above code with a function which will display a message (e.g. Before swapping, After swapping ) followed by the contents of both arrays. You will use it to make sure that our swap_arrays function is working properly. This function should have the following prototype; void display_arrays(char* message, int data1[], int data2[], int size);

The output of your code should look as follows; Initial values; Array #1: [0][0][0] Array #2: [1][1][1] Swapped values; Array #1: [1][1][1] Array #2: [0][0][0] This swap function is working, however we didn t use & when calling it with the a1 and a2 parameters. Why does it work then Swapping arrays with pointers (works just fine) What about switching the parameters types to pointers on int void swap_arrays_two ( int* p1, int* p2, int size ) int i, tmp ; for ( i = 0 ; i < size ; i++ ) tmp = p1[i] ; p1[i] = p2[i] ; p2[i] = tmp ; This swap function is working, however we didn t use & when calling it with the a1 and a2 parameters. Why does it work then

Oddly enough, it seems that C lets us declare our parameters as pointers on int or arrays of int (without size specified). Let s see if this holds true even outside of the context of functions parameters. Add to your main the following code; int another[]; What is the compiler error (cut and paste it below) Does this mean we are allowed to define arrays without size specification when they are not parameters

Swap Arrays, the third Let s now try to implement an integer arrays swap function which does not copy each element. We will attempt to swap the arrays by swapping their start address instead. After all, the name of an array is the same thing as the address of its first element, right So why don t we simply write a function which, given a1 and a2 as parameters, will make sure that after its execution a1 and a2 will now refers to each others contents. Since we are allowed, for functions parameters only, to declare our arrays parameters as pointers on int let s use this to our advantage and treat these arrays as pointers. void swap_arrays_3a ( int* p1, int* p2 ) tmp = p1 ; p1 = p2 ; p2 = tmp ; Implement this function, call it from the main with our two arrays a1 and a2 as parameters then display the arrays contents before / after the function call to check if it s working. Does it work Why yes / no Draw a stack diagram on a separate sheet of paper to come up with a real understanding of the situation.

Let s try for something different. What if we declare our arrays parameters as arrays void swap_arrays_3b ( int p1[], int p2[] ) tmp = p1 ; p1 = p2 ; p2 = tmp ; Does it compile What does this tell you about whether C allows you to assign value to an array directly Does it work Compare the reasons to the previous version (3a) Wait a minute, I thought we couldn t assign values to variable arrays. We know that some things are allowed for array parameters but not for all array variables (cf. above the definition of int another[]; in the main). Let s verify that treating array names, used as parameters, as pointers is or is not a general rule also applying to array variables by adding the following to the main; int another[3]; int * p; p = another; another = p; What is the output of the compiler on this code (cut and paste it below) What are the implications on whether C allows us to assign a value to an array variable

Fast swapping (fourth version) We are going to modify the main of our program so that, instead of trying to swap around the two arrays using their variables names (remember, we can t anyway assign anything to array variables names), we try to obtain a fast swap by working on two pointers on the arrays rather than the arrays themselves. #define SIZE 3 void swap_arrays_3a ( int *p1, int* p2 ); int main ( ) int a1[ SIZE ] = 0,0,0 ; int a2[ SIZE ] = 1,1,1 ; int *p1, *p2; p1 = a1; p2 = a2; swap_arrays_4b ( &p1, &p2 ) ; Let s try first to apply again our 3 rd version of the swap function. void swap_arrays_3a ( int* p1, int* p2 ) tmp = p1 ; p1 = p2 ; p2 = tmp ; The result is expectable. Make sure you understand why calling the function with pointers variables containing the address of the arrays didn t change anything to the situation (draw a stack diagram to clarify this). Now, let s try for something different. First, you need to understand how to write a function which swaps the contents of two integer variables by using pointers. You want

your function to be able to modify a variable parameter by reference, you use pointers on the type of this variable and pass its address by value during the call. Well, here, we d like to enable our swap function to change the contents of the two pointers p1 and p2 (local variables from the main) which are its parameters during the call. To be able to modify an int you need pointer on int parameters. To modify a pointer on int you need pointers on pointers on int parameters. Similarly, you call the function with the address of an int variable you want it to be able to modify. Here, you ll call our function with the address of the pointer on int you want to modify. Implement the following and use it from the main; void swap_arrays_4a ( int** p1, int** p2 ) tmp = *p1 ; *p1 = *p2 ; *p2 = tmp ; Does it work Explain, in a detailed manner by using stack diagrams, what happens during the call of this function.