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

Similar documents
DECLARAING AND INITIALIZING POINTERS

Computer Programming Lecture 12 Pointers

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

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

Slides adopted from T. Ferguson Spring 2016

Computer Programming: C++

Principles of Programming. Chapter 6: Arrays

Dynamic memory allocation

Intermediate Programming, Spring 2017*

Variation of Pointers

Engineering program development 6. Edited by Péter Vass

Pointers. Memory. void foo() { }//return

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Arrays and Pointers in C. Alan L. Cox

Pointers, Dynamic Data, and Reference Types

Introduction to Scientific Computing and Problem Solving

Fundamental of Programming (C)

Understanding Pointers

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Lecture 04 Introduction to pointers

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Fundamentals of Programming Session 20

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

PROGRAMMAZIONE I A.A. 2017/2018

Language comparison. C has pointers. Java has references. C++ has pointers and references

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

AMCAT Automata Coding Sample Questions And Answers

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

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

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

[0569] p 0318 garbage

APSC 160 Review Part 2

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

Pointers in C/C++ 1 Memory Addresses 2

by Pearson Education, Inc. All Rights Reserved.

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Chapter 6 - Pointers

Programming. Pointers, Multi-dimensional Arrays and Memory Management

C Pointers. 6th April 2017 Giulio Picierro

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

More Arrays. Last updated 2/6/19

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Functions. Arash Rafiey. September 26, 2017

CS61C : Machine Structures

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

C Pointers. 7.2 Pointer Variable Definitions and Initialization

POINTER & REFERENCE VARIABLES

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Fundamentals of Programming Session 20

EM108 Software Development for Engineers

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Lecture 05 POINTERS 1

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Basic and Practice in Programming Lab7

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

CSC 211 Intermediate Programming. Arrays & Pointers

What have we learned about when we learned about function parameters? 1-1

Lesson 7. Reading and Writing a.k.a. Input and Output

Intermediate Programming, Spring 2017*

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS61C : Machine Structures

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Course organization. Course introduction ( Week 1)

Dynamic Memory Allocation

ESC101N Fundamentals of Computing

First of all, it is a variable, just like other variables you studied

Memory, Data, & Addressing II CSE 351 Spring

At the end of this module, the student should be able to:

Pointers. 10/5/07 Pointers 1

C Pointers Pearson Education, Inc. All rights reserved.

CS 61c: Great Ideas in Computer Architecture

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

ESC101N: Fundamentals of Computing End-sem st semester

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Fundamentals of Programming Session 19

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

C++ Programming Chapter 7 Pointers

Content. In this chapter, you will learn:

Function I/O. Last Updated 10/30/18

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Transcription:

Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk

Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions When Pointers meet Arrays Arrays & Memory Address Passing Arrays to Functions

Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions When Pointers meet Arrays Arrays & Memory Address Passing Arrays to Functions

What is Pointer Where do variables store Memory (Stack, Heap, Static, ) Address in memory Byte-addressable Each address identifying a single 8 bit of storage (larger data reside in multiple bytes occupying a sequence of consecutive address) E.g., Memory address of the variable C is 0400.

What is Pointer &c denotes the address of the variable c in memory, say, 0400. scanf( %c, &c); The input character, say A, will be stored in the memory address of c So variable c is A after the statement;

Definition of Pointers A ptr is another variable, storing the memory address of the variable c

Definition of Pointers Declare the variable ptr char *ptr; OR char* ptr; Variable name is ptr, Not *ptr The * denotes that ptr is a pointer The type of ptr is pointer to char, or char pointer Variable ptr is to be used to store the memory address of another char variable

Definition of Pointers A c: A &c: 0400 ptr: 0400 *ptr: A &ptr: 0123

Definition of Pointers A c: A &c: 0400 ----- Pointer ptr: 0400 ------ Pointer *ptr: A &ptr: 0123 ----- Pointer Q: *&ptr? &*ptr?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 10

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 7 b 7 p? Declare the two integer variables a and b, and the integer pointer variable p. 11

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 7 b 7 Store the address of variable a into pointer p. p 12

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 7 b 7 p Now, we can use pointer p to access the value of variable a by using the indirection (or called the dereferencing) operator *. *p refers to the value of the variable to which p points, i.e., a 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 3 b 7 When *p appears on the LHS of an assignment, it means the value on the RHS is to be written onto the memory location to which p points. p 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 3 b 7 p Update pointer p to store the address of variable b. 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 3 b 7 p The actual calculation performed is 2 7 3. Re-read line 11 as: b = 2 * b a; 16

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main(void) { int a = 7, b = 7, *p; p = &a; printf( a = %d\n, *p); *p = 3; printf( a = %d\n, a); p = &b; *p = 2 * *p a; printf( b = %d\n, b); return (0); } a = 7 a = 3 b = 11 a 3 b 11 p The actual calculation yields eleven. Re-read line 11 as: b = 2 * b a; 17

Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions When Pointers meet Arrays Arrays & Memory Address Passing Arrays to Functions

How to use Pointers Pointers Assignments One pointer can be assigned to another only when both pointers have the same type. int x, *ptr1, *ptr2; char c, *ptr3; ptr1 = &x; /* valid */ ptr2 = ptr1; /* valid */ ptr3 = &c; /* valid */ ptr2 = ptr3; /* invalid */

How to use Pointers The void Pointer Pointer assignment is allowed when one of the operands is of type pointer to void We treat void* as a generic/ universal pointer type int x, *xptr; char *cptr; void *vptr; xptr = &x; vptr = xptr; /* valid */ cptr = vptr; /* valid */

Call-by-Value & Call-by-Address Functions Call-by-Value Functions Input: 4 5 Output: Result = 9

Call-by-Value Functions Another example What s the output? a = 0, b = 100 Why?

Call-by-Value Functions Another example a 0 b 100 x y

Call-by-Value Functions Another example a 0 b 100 x 0 y 100

Call-by-Value Functions Another example a 0 b 100 x 100 y 0

Call-by-Value Functions The formal parameters x and y (the local variables of swap()) are created when the function is entered, and are destroyed (free from the memory) when the function returns/ terminates No matter how the value of the formal parameters changes, the variable in the calling environment (main() in this example) are never changed

Call-by-Address Functions

Call-by-Address Functions Another example What s the output? a = 100, b = 0 Success!!!

Call-by-Address Functions Another example a 0400 0404 0 b 100 x y

Call-by-Address Functions Another example a 0400 0404 0 b 100 x 0400 y 0404

Call-by-Address Functions Another example a 0400 0404 100 b 0 x 0400 y 0404

Call-by-Address Functions The address of the actual parameters (the variables a and b in main()) are passed to the function The functions applies dereference operator * on the received addresses (stored in pointer variable x and y) to access/ modify the remote variables in the calling environment (main() in this example) indirectly. Therefore, dereference is also called indirection

Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions When Pointers meet Arrays Arrays & Memory Address Passing Arrays to Functions

When Pointers Meet Arrays One-Dimensional Arrays E.g., int list[5]; Assumption 4-byte integers the array is stored at memory address 1000 onwards

One-Dimensional Arrays An array name without index denotes the address of the first element of the array list == &list[0] (base address) i.e., list == 1000 *list == *&list[0] == list[0] list + 1 == &list[1] *(list +1) == *&list[1] == list[1] Starting address of list[i] is given by &list[i] = base address + sizeof(int) i = 1000 + 4i

One-Dimensional Arrays String: One-Dimensional Character Arrays char s[] = abc ; char s[3] = { a, b, c }; char *s = abc ;

One-Dimensional Arrays String: One-Dimensional Character Arrays char s[] = abc ; char s[3] = { a, b, c }; char *s = abc ; ---- Read Only! Place abc in the read-only part of the memory, and make s a pointer to that. *s == a ; char s[] = abc ; char *t = &s; ----- Writable!

Two-Dimensional Arrays int matrix[3][4]; An array name without index denotes the address of the first element of the array matrix == &matrix[0][0] For int matrix[row][col], starting address of matrix[i][j] is given by Base address + sizeof(int) (COL i + j) E.g. &matrix[2][3] = 1000 + 4 (4 2 + 3) =1044

Passing Arrays to Functions Output list[0] = 2 list[1] = 4 list[2] = 6 list[3] = 8 list[4] = 10

Passing 1-D Arrays to Functions In the calling environment main() The name of the array is used as the parameter The bracket pair [ ] must be omitted In the called function times2() The bracket pair is required, which is to tell the complier that the parameter of this function is an array The array size can be omitted

Passing Multi-D Arrays to Functions In the calling environment The name of the array is used as the parameter The bracket pairs [ ] [ ] [ ] must be omitted E.g., times2 ( array3d ); In the called function The bracket pairs [ ] [ ] [ ] are required to tell the compiler that the parameter is a multi-dimensional array The first dimension s size can be omitted, while the all the other dimension s size must be specified. E.g., void times2 ( array3d[ ][10][20] ) { }

Passing Arrays to Functions Call-by-Address Mechanism In the calling environment main(), after calling times2(), elements in list[ ] are doubled. Why? The array list[ ] is not copied to the function times2(). No new array is created. Instead, a[ ] in times2() refers to the same array list[ ] in main() Thus modifying any element in a[ ] causes corresponding modifications on list[ ] actually. Call-by-Address!

Passing Arrays to Functions An Alternative View

Practice 1.Array Swap Let A[10] and B[10] be two arrays int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int B[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; Write a function to swap the elements of A and B and write another function to print the result

Practice 2. Reverse String Let S be a string: char S[] = nametag ; Write a function to reverse the elements in S and write another function to print S E.g., ABCDE -> EDCBA

Thanks