Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

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

Arrays and Pointers (part 1)

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers (part 1)

Pointers. Pointer References

Pointers. Pointers. Pointers (cont) CS 217

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

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

SYSC 2006 C Winter 2012

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

[0569] p 0318 garbage

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

... Lecture 12. Pointers

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

Goals of this Lecture

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

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera

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


Pointers as Arguments

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Multidimension array, array of strings

Lectures 5-6: Introduction to C

2.2 Pointers. Department of CSE

Arrays, Pointers and Memory Management

Lectures 5-6: Introduction to C

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Computer Programming: Skills & Concepts (CP) Strings

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Computer Programming. Pointers. Marius Minea. 20 November 2017

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

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

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

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

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lecture 2: C Programm

by Pearson Education, Inc. All Rights Reserved.

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

CS1100 Introduction to Programming

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

REFERENCES, POINTERS AND STRUCTS

CSE2301. Arrays. Arrays. Arrays and Pointers

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

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

MYcsvtu Notes LECTURE 34. POINTERS

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Basic and Practice in Programming Lab7

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Systems Programming and Computer Architecture ( )

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

Advanced Pointer Topics

Pointers. 10/5/07 Pointers 1

Introduction to C++ with content from

Memory, Arrays & Pointers

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Array Initialization

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

High Performance Programming Programming in C part 1

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Crash Course into. Prof. Dr. Renato Pajarola

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

Dynamic Allocation in C

Exercise Session 2 Simon Gerber

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

ME964 High Performance Computing for Engineering Applications

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

Fundamental of Programming (C)

Fundamentals of Programming Session 20

A pointer is just a variable with its value being an address of another variable. It is similar to the concept of reference in Java.

CSE 333 Midterm Exam 5/10/13

Character Strings. String-copy Example

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Programming in C++ 5. Integral data types

Intermediate Programming, Spring 2017*

Principles of C and Memory Management

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers

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

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CS 61c: Great Ideas in Computer Architecture

Pointers, Dynamic Data, and Reference Types

BITG 1113: POINTER LECTURE 12

CS61C : Machine Structures

Lecture 3: C Programm

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

12. Pointers Address-of operator (&)

Transcription:

Week 5 9 April 2018 NWEN 241 More on pointers Alvin Valera School of Engineering and Computer Science Victoria University of Wellington

Admin stuf Exercises (Set 3): Covers Weeks 4-5 lessons Download from NWEN 241 course wiki Solutions will be released on Friday (13 Apr 2018) Some of the questions will be discussed in Tutorial NWEN 241: Systems Programming 2

Admin stuf Clarifications on Programming Assignment 2: How to deal with a user input that is already taken You can either (a) skip player and go to the other player, or (b) force the player to enter an untaken box Important thing is your program should handle them consistently and not crash when such exceptions occur How to deal with a user input that is out of bounds or invalid Force the player to enter a value that is within bounds. If user enters value that is already taken, then apply (a) or (b) NWEN 241: Systems Programming 3

Recap: pointer basics A pointer is a variable that contains memory address A pointer points to a memory location To obtain value of data pointed to by pointer, need to perform indirection or dereferencing NWEN241 Systems Programming 4

Recap: pointer-related operators Address operator (&) The operation will return the memory location of the variable Can be applied on any variable, including pointers Indirection operator (*) The operation will return the value of data pointed to by pointer Can only be applied on pointer variables NWEN241 Systems Programming 5

Recap: pointer arithmetic Addition and subtraction can be performed on pointer variables Result depends on data type pointed to by pointer Suppose : data_type *name; name + k name - k Evaluated as name + k*sizeof(data_type) Evaluated as name - k*sizeof(data_type) NWEN241 Systems Programming 6

Recap: Usage of pointers 1) Provide an alternative means of accessing information stored in arrays 2) Provide an alternative (and more eficient) means of passing parameters to functions 3) Enable dynamic data structures, that are built up from blocks of memory allocated from the heap at run time NWEN241 Systems Programming 7

Traversing arrays using pointers The usual way to iterate over arrays: int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int i = 0; i < len; i++) { /* Do something about a[i] */ Using pointers: int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int *ip = a; ip < a + len; ip++) { /* Do something about *ip */ NWEN241 Systems Programming 8

Traversing arrays using pointers int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int *ip = a; ip < a + len; ip++) { /* Do something about *ip */ 1 st iteration: a... ip NWEN241 Systems Programming 9

Traversing arrays using pointers int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int *ip = a; ip < a + len; ip++) { /* Do something about *ip */ 2 nd iteration: a... ip NWEN241 Systems Programming 10

Traversing arrays using pointers int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int *ip = a; ip < a + len; ip++) { /* Do something about *ip */ 3 rd iteration: a... ip NWEN241 Systems Programming 11

Traversing arrays using pointers int a[] = {... ; int len = sizeof(a)/sizeof(int); for(int *ip = a; ip < a + len; ip++) { /* Do something about *ip */ (len-1)th iteration: a... ip NWEN241 Systems Programming 12

A note on operator precedence Previously, operator precedence table from Kernighan & Ritchie: NWEN241 Systems Programming 13

A note on operator precedence Slight correction: These only refer to prefix ++ and -- Postfix ++ and has level 1 precedence, i.e., the same as (), [], -> and. NWEN241 Systems Programming 14

Increment and indirection together Suppose int *ip; int i; What does i = *ip++ mean? Since ++ has higher precedence than *, the RHS expression evaluates to *(ip++) which means i = *ip; ip = ip + 1; NWEN241 Systems Programming 15

Increment and indirection together Suppose int *ip; int i; What does i = *++ip mean? Both ++ and * have same precedence, so associativity is applied on RHS yielding *(++ip) which means ip = ip + 1; i = *ip; NWEN241 Systems Programming 16

Increment and indirection together Suppose int *ip; int i; How to increment the value of whatever ip points to? (*ip)++; NWEN241 Systems Programming 17

Strings Pointers int strlen (char *s) { int n; for(n=0; *s!='\0'; s++) n++; return n; Notice in the second strcmp() and second and third strcpy(), the use of pointers to iterate through the strings int strcmp(char *s, char *t) { int i; for(i=0;s[i]==t[i];i++) if(s[i] == '\0') return 0; return s[i] t[i]; int strcmp(char *s, char *t) { for(;*s == *t; s++,t++) if (*s == '\0') return 0; return *s - *t; void strcpy(char *s, char *t) { int i = 0; while((s[i]=t[i])!= '\0') i++; void strcpy(char *s, char *t) { while((*s=*t)!= '\0') { s++; t++; void strcpy(char *s, char *t) { while((*s++=*t++)!= '\0'); The conciseness of the last strcmp() and strcpy() make them hard to understand NWEN241 Systems Programming 18

A closer look at strcmp() int strcmp(char *s, char *t) { for(;*s == *t; s++,t++) if (*s == '\0') return 0; return *s - *t; What is the meaning of the return value? If strings are the same, return value is 0 If strings are not the same, return value is *s - *t Return value indicates lexicographical order of s and t: Positive if s appears afer t, negative otherwise NWEN241 Systems Programming 19

Reference / variable parameters To make changes to a variable that exist afer a function ends, we pass the address of the variable to the function (a reference parameter) Then we use indirection operator inside the function to change the value the parameter points to: int a, b; swap(&a, &b); void swap(int *px, int *py) { int tmp; tmp = *px; *px = *py; // values stored at *py = tmp; // addresses of 'a' // and 'b' are swapped NWEN241 Systems Programming 20

Returning pointers from functions A function can also return a pointer value: float *find_max(float A[], int N) { int i; float *the_max = &(A[0]); for (i = 1; i < N; i++) if (A[i] > *the_max) the_max = &(A[i]); return the_max; int main(void) { float scores[5] = {10.0, 8.0, 5.5, 2.0, 4.1; float *max_score; max_score = find_max(scores, 5); printf("%.1f\n",*max_score); return 0; NWEN241 Systems Programming 21

Returning pointers from functions Caution!!! In functions, do not do return p, where p is a pointer to a local variable. This is because local variables are de-allocated when the function ends so whatever p is pointing to will no longer be available but if you return the pointer, then you still are pointing at that memory location even though you no longer know what is there Question: Why is it allowed in the previous example? NWEN241 Systems Programming 22

Pointer to pointers A pointer can also be made to point to a pointer variable (but the pointer must be of a type that allows it to point to a pointer) Example: int V = 101; int *P = &V; /* P points to int V */ int **Q = &P; /* Q points to int pointer P */ printf("%d %d %d\n", V, *P, **Q); /* prints 101 3 times */ NWEN241 Systems Programming 23

Pointer arithmetic Assume short is 2 bytes, and pointer variable (address size) is 4 bytes. short a[10]={5, 10, 15, ; short *pa, **ppa; int i=5; pa = &a; ppa = &pa; ppa 800 700 700 pa 640 Variable Address Value a 640 5 642 10 644 15 640 642 644 646 648 650 a 5 10 15 pa 700 640 ppa 700 700 Questions: Expression Value Note pa+1 642 640+1*2 pa+3 646 640+3*2 pa+i 650 640+i*2 *pa+1 6 5+1 *(pa+1) 10 a[1]=pa[1]=*(a+1) pa[2] 15 644 *ppa 640 Value of pa *ppa+1 642 pa+1 *(*ppa+1) 10 *(pa+1) *(ppa+1) invalid *(704) NWEN241 Systems Programming 24

Pointer types Pointers are generally of the same size, but it is inappropriate to assign an address of one type of variable to a diferent type of pointer Example: int V = 101; float *P = &V; /* generally results in a warning */ Warning rather than error because C will allow you to do this (it is appropriate in certain situations) NWEN241 Systems Programming 25

Casting pointers When assigning a memory address of a variable of one type to a pointer that points to another type, it is best to use the cast operator to indicate the cast is intentional (this will remove the warning). Example: int V = 101; float *P = (float *) &V; /* Casts int address to float * */ Removes warning, but is still unsafe to do this!!! NWEN241 Systems Programming 26

General (void) pointer A void * is considered to be a general pointer No cast is needed to assign an address to a void * or from a void * to another pointer type Example: int V = 101; void *G = &V; /* No warning */ float *P = G; /* No warning, still unsafe */ Certain library functions return void * results NWEN241 Systems Programming 27

Pointers and const Const pointer: cannot be reassigned to point to a diferent location from the one it is initially assigned, but it can be used to modify the location that it points to data_type * const p; Pointer to a const location: can be reassigned to point to a diferent location, but it cannot be used to modify any location that it points to const data_type * p; Const pointer to a const location: can neither be reassigned nor used to modify the location that it points to const data_type * const p; NWEN241 Systems Programming 28

Pointer to function Function code is stored in memory Functions also occupy memory locations therefore every function has an address just like variables Just like ordinary variables, the address of a function refers to its starting address C does not require that pointers only point to data, it is possible to have pointers to functions NWEN241 Systems Programming 29

Defining a function pointer Declaration: return_type (*name)(param_types); Examples int (*f)(int, float); int *(*f)(int, float); Pointer to a function that takes an int and float arguments, resp., and returns an int Pointer to a function that takes an int and float arguments, resp., and returns a pointer to int NWEN241 Systems Programming 30

Using a function pointer /* f is a function pointer */ int (*fp)(int, float); int F1(int i, float f) { return i/f; int main(void) { /* Assignment: let f point to F1 */ fp = &F1; /* fp = F1; is also ok */ /* Invocation */ float a = fp(1, 2.0); /* This is equivalent to calling F1(1, 2.0) */ NWEN241 Systems Programming 31

Comparing function pointers Can use the equality (==) operator Example: /* f is a function pointer */ int (*fp)(int, float); int F1(int i, float f) { return i/f; int main(void) { /* Assignment: let f point to F1 */ fp = &F1; /* fp = F1; is also ok */ if(fp == &F1) printf("points to F1\n"); NWEN241 Systems Programming 32

Safety concerns What if uninitialized function pointer value is accessed Safest outcome: memory error, and program is terminated But what if the garbage value is a valid address? Worst case: address contains program instruction execution continues, with random results Hard to trace the cause of the erroneous behavior NWEN241 Systems Programming 33

Usage of function pointers For implementing callback functions Function pointer is passed as an argument to a function The function will then invoke the passed function pointer at a given time void qsort(void *base, size_t nitems, size_t size, int (*compare)(const void *, const void*)); base Pointer to the first element of the array to be sorted nitems Number of elements in the array pointed by base size Size in bytes of each element in the array. compare This is the function that compares two elements. NWEN241 Systems Programming 34

Next lecture Storage classes NWEN241 Systems Programming 35