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

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

Lecture 2: C Programm

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

ECE 15B COMPUTER ORGANIZATION

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

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

CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

HW1 due Monday by 9:30am Assignment online, submission details to come

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

More C Pointer Dangers

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

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

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

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

CS61C : Machine Structures

CS61C : Machine Structures

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

CS61C : Machine Structures

C Arrays, Strings, More Pointers Instructor: Steven Ho

CS61C : Machine Structures

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

Arrays and Pointers (part 1)

Has there been an update to ANSI C? Lecture #3 C Pointers Yes! It s called the C99 or C9x std

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

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

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

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

Arrays and Pointers (part 1)

Lecture 3: C Programm

Memory, Data, & Addressing II CSE 351 Spring

Pointers and Arrays 1

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS61C : Machine Structures

Arrays and Pointers in C. Alan L. Cox

Multidimension array, array of strings

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

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

Memory, Arrays & Pointers

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

Array Initialization

REFERENCES, POINTERS AND STRUCTS

CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

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

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Intermediate Programming, Spring 2017*

Introduction to Linked Lists

Pointers. Pointers. Pointers (cont) CS 217

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

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


Systems Programming and Computer Architecture ( )

[0569] p 0318 garbage

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

MYcsvtu Notes LECTURE 34. POINTERS

EL2310 Scientific Programming

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

EM108 Software Development for Engineers

Lectures 5-6: Introduction to C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

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

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Understanding Pointers

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Heap Arrays and Linked Lists. Steven R. Bagley

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

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

POINTER AND ARRAY SUNU WIBIRAMA

Personal SE. Functions, Arrays, Strings & Command Line Arguments

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

UNIT V Sub u P b ro r g o r g a r m a s

Pointers, Arrays, and Strings. CS449 Spring 2016

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays

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

n Address of a variable in memory n Allows us to indirectly access variables ! Array n A list of values arranged sequentially in memory

Transcription:

Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in memory (difference with arrays in Java) B. Arrays and Pointers C. Fast data access with arrays D. Array pitfalls III. Pointer arithmetic IV. Strings

Pointers Pointer: A variable that contains the address of a variable How to create a pointer: int *x, y; 102 120 x"?" y"?" 102 120 y = 3; x"?" y" 3" 102 120 x = &y; x" 120! y" 3" sizeof(x)=? x points to y 2

How to change the value of a variable pointed to by a pointer? Use dereference * operator to left of pointer name x" y" 3" *x = 5; x" y" 5" Two ways of changing the value of any variable Why this is useful will be clear when we discuss functions and pointers 3

Pointer and Pointee data types Q: This code gives a warning at compile time. Why? main() { int *p; float y; p = &y; printf( Pointer points to value %d,*p); } A. The pointer p is made to point to a variable of incompatible type B. The printf statement expects an integer but p is pointing to a type 'loat C. *p does not contain a valid value because y was not initialized 4

Pointer and Pointee data types Q: This code gives a warning at compile time. Why? main() { int *p; float y; p = &y; printf( Pointer points to value %d,*p); } A. The pointer p is made to point to a variable of incompatible type B. The printf statement expects an integer but p is pointing to a type 'loat C. *p does not contain a valid value because y was not initialized 5

Type casting with pointers We can cast addresses that are assigned to pointers to a different type char c =0x04; int *p; p= (int *)&c; /* Treat &c as address of an int */ OR short i =0x0104; char *p; p= (char *)&i; /* Treat &i as address of a char*/

What is the output of the char *p; short i =0x0104; following code? p= (char *)&i; /* Treat &i as address of a char*/ printf( Value in p is: %x,*p); A. It is always the lower byte 0x04 B. It is always the higher byte 0x01 C. Depends

What is the output of the char *p; short i =0x0104; following code? p= (char *)&i; /* Treat &i as address of a char*/ printf( Value in p is: %x,*p); A. It is always the lower byte 0x04 B. It is always the higher byte 0x01 C. Depends: Little endian 0x04, Big endian 0x01 Difference with Java references: Pointers in C have a more generic usage

Q: Does the following code give an error at run time? If yes, why? If no, what is the output? main() { int *p; *p = 5; printf( %d,(*p)++); } A. Yes, because the pointer does not point to a valid address in memory B. Yes, because there is a syntax error in the printf statement: (*p)++ is not a valid operation on a pointer C. No, the output of the program is the value pointed to by p, which is 5 D. No, the output of the program is the value pointed to by p, plus one, which is 6 9

Q: Does the following code give an error at run time? If yes, why? If no, what is the output? main() { int *p; *p = 5; printf( %d,(*p)++); } A. Yes, because the pointer does not point to a valid address in memory B. Yes, because there is a syntax error in the printf statement: (*p)++ is not a valid operation on a pointer C. No, the output of the program is the value pointed to by p, which is 5 D. No, the output of the program is the value pointed to by p, plus one, which is 6 10

Two important facts about Pointers 1) A pointer can only point to one type (basic or derived ) such as int, char, a struct, another pointer, etc 2) After declaring a pointer: int *ptr; ptr doesn t actually point to anything yet. We can either: Ø make it point to something that already exists, or Ø allocate room in memory for something new that it will point to (next lecture) 11

Consider the following function void swap(int x, int y); //Declaration void swap(int x, int y) { int tmp; tmp= x; x= y; y= tmp; } tmp x swap y The swap function intends to swap the value of its inputs. Is the logic correct? A. Yes B. No C. Depends x y 12

Q: Are the value of variables a and b interchanged after swap is called? main() {... swap(a, b);.... } a 5 6 b A. Yes, because.. B. No, because.. 13

Functions: Call by value main() {... 5 6 x swap(a, b); a b.... } swap y Q: Are the value of variables a and b interchanged after swap is called? A. Yes, because that s what is implemented by the swap routine B. No, because the inputs to swap are only copies of a and b 14

Q: Which of the following changes are required to interchange the values in a and b when swap is called? {.... swap(a, b); } a 5 6 b x swap y A. In swap, return the values of x and y to the main function after swapping them B. Declare a and b as global variables, so that they become accessible to the swap routine C. Pass the address of a and b to swap instead of their value D. Move the implementation in swap to the main function 15

Q: Which of the following changes are required to interchange the values in a and b when swap is called? {.... swap(a, b); } a 5 6 b x swap y A. In swap, return the values of x and y to the main function after swapping them B. Declare a and b as global variables, so that they become accessible to the swap routine C. Pass the address of a and b to swap instead of their value D. Move the implementation in swap to the main function 16

Functions: Call by reference void swap(int *x, int *y) {... } Q: What should the modified swap function do? A. Swap the addresses in x and y B. Swap the values pointed to by x and y C. Both the above operations are equivalent 17

Functions: Call by reference void swap(int *x, int *y) {... } Q: What should the modified swap function do? A. Swap the addresses in x and y B. Swap the values pointed to by x and y C. Both the above operations are equivalent 18

Array Basics ar 100 104 108 112 116 " 20 Stores group of elements of the same type Declaration: int ar[5]; // declares a 5-element integer array int ar[] = {795, 635}; //declares and fills a 2-element integer array. Accessing elements: ar[i];// returns the (i+1) th element How are arrays in C different from Java? Pointers and arrays are very similar 19

Arrays and Pointers ar 100 104 108 112 116 " 20 ar is a pointer to the first element ar[0] is the same as *ar ar[2] is the same as *(ar+2) Use pointer arithmetic to access arrays more conveniently e.g. when passing arrays to functions

Pointer Arithmetic Since a pointer is just a memory address, we can add to it to traverse an array. ptr+1 will return a pointer to the next array element. ptr 100 104 108 112 116 " 20 40 60 *ptr+1 =? *ptr++ =? *(ptr+1)=? A. 21, 20, 40 B. 21, 21, 40 C. 21, 40, 40

Pointer Arithmetic Since a pointer is just a memory address, we can add to it to traverse an array. ptr+1 will return a pointer to the next array element. ptr *ptr+1 = 21 *ptr++ = 20 *(ptr+1)= 40 100 104 108 112 116 " 20 40 60

Arrays: Fast data access Using pointer arithmetic, easy to compute the address of any array element in memory How are array elements accessed on an ARM? ptr 100 104 108 112 116 " 20 40 60 Base address (100) register Processor Address Data Memory Easy to compute the address of any array element using information in registers

Q: Which of the assignment statements produces an error at compilation. Why? int *p, ar[5]; //Declaration p=ar+5; ar=p+1; ar 100 104 108 112 116 " p 20 A. p=ar+5; B. ar=p+1; C. Both statements result in error at compile time D. Neither results in a compilation error

Q: Which of the assignment statements produces an error at compilation. Why? int *p, ar[5]; //Declaration p=ar+5; ar=p+1; ar 100 104 108 112 116 " p 20 A. p=ar+5; B. ar=p+1; because the address stored in an array cannot be changed C. Both statements result in error at compile time D. Neither results in a compilation error

Q: What happens when the following code is executed? int *p, ar[5]; //Declaration p=ar- 5; *p=0; A. It always results in a segmentation fault because a pointer cannot be used to change the value of an array element B. It always results in a segmentation fault because the array element being accessed is out of bounds C. It is likely to result in a segmentation fault because the memory location being accessed may not be a valid address D. It results in a compilation error

Q: What happens when the following code is executed? int *p, ar[5]; //Declaration p=ar- 5; *p=0; A. It always results in a segmentation fault because a pointer cannot be used to change the value of an array element B. It always results in a segmentation fault because the array element being accessed is out of bounds C. It is likely to result in a segmentation fault because the memory location being accessed may not be a valid address D. It results in a compilation error

Arrays Pitfall: An array in C does not know its own length, & bounds not checked! Consequence: We can accidentally access off the end of an array. Consequence: We must pass the array and its size to a procedure which is going to traverse it. Segmentation faults and bus errors: These are VERY difficult to find, so be careful.

Pointer Arithmetic What if we have an array of large structs (objects)? C takes care of it: In reality, ptr+1 doesn t add 1 to the memory address, but rather adds the size of the array element. C knows the size of the thing a pointer points to every addition or subtraction moves that many bytes: 1 byte for a char, 4 bytes for an int, etc.

Pointer Arithmetic Question How many of the following are invalid? I. pointer + integer (ptr+1) II. integer + pointer (1+ptr) III. pointer + pointer (ptr + ptr) IV. pointer integer (ptr 1) V. integer pointer (1 ptr) VI. pointer pointer (ptr ptr) VII. compare pointer to pointer (ptr == ptr) VIII. compare pointer to integer (1 == ptr) IX. compare pointer to 0 (ptr == NULL) X. compare pointer to NULL (ptr == NULL) #invalid A: 1 B: 2 C: 3 D: 4 E: 5

Pointer Arithmetic Question How many of the following are invalid? I. pointer + integer (ptr+1) II. integer + pointer (1+ptr) III. pointer + pointer (ptr + ptr) IV. pointer integer (ptr 1) V. integer pointer (1 ptr) VI. pointer pointer (ptr ptr) VII. compare pointer to pointer (ptr == ptr) VIII. compare pointer to integer (1 == ptr) IX. compare pointer to 0 (ptr == NULL) X. compare pointer to NULL (ptr == NULL) #invalid A: 1 B: 2 C: 3 D: 4 E: 5

void IncrementPtr(int *p){ p = p + 1; } Q: What happens when IncrementPtr(q)is called in the following code: A q int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(q); 50 60 70 A. The pointer q points to the next element in the array with value 60 B. The pointer q points to the first element in the array with value 50

void IncrementPtr(int *p){ p = p + 1; } Q: What happens when IncrementPtr(q)is called in the following code: A q int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(q); 50 60 70 A. The pointer q points to the next element in the array with value 60 B. The pointer q points to the first element in the array with value 50

void IncrementPtr(int **p){ p = p + 1; } Q: How should we implement IncrementPtr(),so that q moves by one element when the following code executes? int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); A q 50 60 70 A. p = p + 1; //The current one is correct B. &p = &p + 1; C. *p= *p + 1; D. *p++; E. p= &p+1;

void IncrementPtr(int **p){ p = p + 1; } Q: How should we implement IncrementPtr(),so that q moves by one element when the following code executes? int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); A q 50 60 70 A. p = p + 1; //The current one is correct B. &p = &p + 1; C. *p= *p + 1; D. *p++; E. p= &p+1;

C Strings A string in C is just an array of characters. char string[] = abc ; char string[20]; How do you tell how long a string is? Last character is followed by a 0 byte (null terminator) int strlen(char s[]) { int n = 0; while (s[n]!= 0) n++; return n; }

Q: Why can we not copy strings using the function below? void copy (char sto[], char sfrom[]) { sto = sfrom; }" A. sto is an array, therefore its value cannot be changed B. We can use the assignment statement but we have to allocate space for sto prior to the assignment C. The change in the value of sto is not reflected in the calling function

Q: Why can we not copy strings using the function below? void copy (char sto[], char sfrom[]) { sto = sfrom; }" A. sto is an array, therefore its value cannot be changed B. We can use the assignment statement but we have to allocate space for sto prior to the assignment C. The change in the value of sto is not reflected in the calling function

Q: Why can we not compare strings using the function below? int compare(char st1[], char st2[]) { return(st1 == sto);" }" A. We cannot have an arithmetic expression in the return statement B. The function only compares the base address of the strings, not their value C. The function only compares if the first element of the two strings are the same

Q: Why can we not compare strings using the function below? int compare(char st1[], char st2[]) { return(st1 == sto);" }" A. We cannot have an arithmetic expression in the return statement B. The function only compares the base address of the strings, not their value C. The function only compares if the first element of the two strings are the same

C String Standard Functions int strlen(char *string); compute the length of string int strcmp(char *str1, char *str2); return 0 if str1 and str2 are identical (how is this different from str1 == str2?) int strcpy(char *dst, char *src); copy the contents of string src to the memory at dst. The caller must ensure that dst has enough memory to hold the data to be copied. Defined in the header file string.h