C Arrays, Strings, More Pointers Instructor: Steven Ho

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

CS 61c: Great Ideas in Computer Architecture

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

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

CS61C : Machine Structures

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

ECE 15B COMPUTER ORGANIZATION

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

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

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

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

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

CS61C : Machine Structures

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

More C Pointer Dangers

CS61C : Machine Structures

CS61C : Machine Structures

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

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

Arrays and Memory Management

CS61C : Machine Structures

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

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

CS61C : Machine Structures

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

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

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?!

Arrays and Pointers in C. Alan L. Cox

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

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

CS61C : Machine Structures

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

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Lecture 2: C Programm

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

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

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

CS 61c: Great Ideas in Computer Architecture

Intro to C: Pointers and Arrays

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

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

Lectures 5-6: Introduction to C

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

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

Lectures 5-6: Introduction to C

Intermediate Programming, Spring 2017*

Pointers in C/C++ 1 Memory Addresses 2

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

Arrays and Pointers (part 1)

CS 61C: Great Ideas in Computer Architecture Introduction to C

Arrays and Pointers (part 1)

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Lectures 13 & 14. memory management

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

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

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Multidimension array, array of strings

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

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

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

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

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Lecture #6 Intro MIPS; Load & Store

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Memory, Arrays & Pointers

C++ for Java Programmers

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

CS61C : Machine Structures

Array Initialization

CS61C : Machine Structures

Kurt Schmidt. October 30, 2018

Homework #3 CS2255 Fall 2012

Programming in C - Part 2

Fundamental of Programming (C)

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

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

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

CS 222: Pointers and Manual Memory Management

Variables and literals

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

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Pointers and Arrays 1

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

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

Lecture 04 Introduction to pointers

Transcription:

C Arrays, Strings, More Pointers Instructor: Steven Ho

Review of Last Lecture C Basics Variables, Functions, Flow Control, Types, and Structs Only 0 and NULL evaluate to FALSE Pointers hold addresses Address vs. Value Allow for efficient code, but prone to errors C functions pass by value Passing pointers circumvents this 6/21/2017 CS61C Su18 - Lecture 3 2

Struct Clarification Structure definition: Does NOT declare a variable Variable type is struct foo struct foo name1, *pn, name_ar[3]; Joint struct definition and typedef Don t need to name struct in this case struct foo { /* fields */ }; struct foo { /* fields */ }; typedef struct foo bar; bar name1; typedef struct foo { /* fields */ } bar; bar name1; 6/21/2017 CS61C Su18 - Lecture 3 3

sizeof() Integer and pointer sizes are machine dependent how do we tell? Use sizeof() function Returns size in bytes of variable or data type name Examples: int x, *y; sizeof(x); // 4 (32-bit int) sizeof(int); // 4 (32-bit int) sizeof(y); // 4 (32-bit addr) sizeof(char); // 1 (always) 6/20/2017 CS61C Su18 Lecture 2 4

sizeof() Acts differently with arrays and structs (to be explained later) Arrays: returns size of whole array Structs: returns size of one instance of struct (sum of sizes of all struct variables + padding) eg: typedef struct example { char *name; int year; } Song; Song song1; sizeof(song1); // 4 + 4 = 8 6/20/2017 CS61C Su18 Lecture 2 5

Question: What is the result from executing the following code? #include <stdio.h> int main() { int *p; *p = 5; printf( %d\n,*p); } (A) (B) (C) (D) Prints 5 Prints garbage Always crashes Almost always crashes 6

Great Idea #1: Levels of Representation/Interpretation Higher-Level Language Program (e.g. C) Assembly Language Program (e.g. RISCV) Machine Language Program (RISCV) Machine Interpretation Hardware Architecture Description (e.g. block diagrams) Architecture Implementation Compiler Assembler Logic Circuit Description (Circuit Schematic Diagrams) temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw x5, 0(x2) lw x6, 4(x2) sw x6, 0(x2) sw x5, 4(x2) We are here_ 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 6/21/2017 CS61C Su18 - Lecture 3 7

Agenda C Operators Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 8

Operator Precedence 6/21/2017 CS61C Su18 - Lecture 3 9

Assignment and Equality One of the most common errors for beginning C programmers a = b is assignment a == b is equality test Comparisons use assigned value if (a=b) is true if a 0 after assignment (b 0) 6/21/2017 CS61C Su18 - Lecture 3 10

Operator Precedence For precedence/order of execution, see Table 2-1 on p. 53 of K&R Use parentheses to manipulate Equality test (==) binds more tightly than logic (&,, &&, ) x&1==0 means x&(1==0) instead of (x&1)==0 6/21/2017 CS61C Su18 - Lecture 3 11

Operator Precedence For precedence/order of execution, see Table 2-1 on p. 53 of K&R Prefix (++p) takes effect immediately Postfix/Suffix (p++) takes effect last int main () { int x = 1; int y = ++x; // y = 2, x = 2 x--; int z = x++; // z = 1, x = 2 return 0; } 6/21/2017 CS61C Su18 - Lecture 3 12

Agenda C Operators Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 13

Array Basics Declaration: int ar[2]; declares a 2-element integer array (just a block of memory) int ar[] = {795, 635}; declares and initializes a 2-element integer array Accessing elements: ar[num] returns the num th element Zero-indexed 6/21/2017 CS61C Su18 - Lecture 3 14

Arrays Basics Pitfall: An array in C does not know its own length, and its bounds are not checked! We can accidentally access off the end of an array We must pass the array and its size to any procedure that is going to manipulate it Mistakes with array bounds cause segmentation faults and bus errors Be careful! These are VERY difficult to find (You ll learn how to debug these in lab) 6/21/2017 CS61C Su18 - Lecture 3 15

Accessing an Array Array size n: access entries 0 to n-1 Use separate variable for array declaration & array bound to be reused (eg: no hard-coding) Bad pattern Bad Pattern Better Pattern int i, ar[10]; for(i=0; i<10; i++) {...} Better pattern int ARRAY_SIZE = 10; int i, ar[array_size]; for(i=0; i<array_size; i++) {...} Single source of truth! 6/21/2017 CS61C Su18 - Lecture 3 16

Arrays and Pointers Arrays are (almost) identical to pointers char *string and char string[] are nearly identical declarations Differ in subtle ways: initialization, sizeof(), etc. Key Concept: An array variable looks like a pointer to the first (0 th ) element ar[0] same as *ar; ar[2] same as *(ar+2) We can use pointer arithmetic to conveniently access arrays An array variable is read-only (no assignment) (i.e. cannot use ar = <anything> ) 6/21/2017 CS61C Su18 - Lecture 3 17

Array and Pointer Example ar[i] is treated as *(ar+i) To zero an array, the following three ways are equivalent: 1) for(i=0; i<size; i++) ar[i] = 0; 2) for(i=0; i<size; i++) *(ar+i) = 0; 3) for(p=ar; p<ar+size; p++) *p = 0; These use pointer arithmetic, which we will get to shortly 6/21/2017 CS61C Su18 - Lecture 3 18

Arrays Stored Differently Than void foo() { int *p, a[4], x; p = &x; Pointers } *p = 1; // or p[0] printf("*p:%u, p:%u, &p:%u\n",*p,p,&p); *a = 2; // or a[0] printf("*a:%u, a:%u, &a:%u\n",*a,a,&a); 0 4 8 12 16 20 24 28 32 36 40 44 48 40 2 1 p x...???...? 24 a *p:1, p:40, &p:20 *a:2, a:24, &a:24 K&R: An array. name is not a variable. 6/21/2017 CS61C Su18 - Lecture 3 19

Arrays and Functions Declared arrays only allocated while the scope is valid: char *foo() { char string[32];...; return string; } BAD An array is passed to a function as a pointer: Really int *ar int foo(int ar[], unsigned int size) {... ar[size-1]... } Must explicitly pass the size! 6/21/2017 CS61C Su18 - Lecture 3 20

Arrays and Functions Array size gets lost when passed to a function What prints in the following code: int foo(int array[], unsigned int size) {... printf( %d\n, sizeof(array)); } int main(void) { int a[10], b[5];... foo(a, 10)... printf( %d\n, sizeof(a)); } sizeof(int??? *) 10*sizeof(int)??? 6/21/2017 CS61C Su18 - Lecture 3 21

Agenda C Operators Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 22

Administrivia Disc1 today, Lab1 tomorrow To rent iclicker from us, bring check to lab! HW0 and mini-bio due Monday HW1 will be released Thursday, so be prepared! Proj1 (Chatroom) will be released tonight, due June 29 6/21/2017 CS61C Su18 - Lecture 3 23

Agenda C Operators Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 24

C Strings String in C is just an array of characters char string[] = "abc"; Last character is followed by a 0 byte ( \0 ) (a.k.a. null terminator ) How do you tell how long a string is? int strlen(char s[]) { int n = 0; while (s[n]!= 0) n++; return n; } Array size here is 4 This means you need an extra space in your array!!! 6/21/2017 CS61C Su18 - Lecture 3 25

C String Standard Functions Accessible with #include <string.h> int strlen(char *string); Returns the length of string (not including null term) int strcmp(char *str1, char *str2); Return 0 if str1 and str2 are identical (how is this different from str1 == str2?) char *strcpy(char *dst, char *src); Copy contents of string src to the memory at dst. Caller must ensure that dst has enough memory to hold the data to be copied Note: dst = src only copies pointer (the address) 6/21/2017 CS61C Su18 - Lecture 3 26

String Examples #include <stdio.h> #include <string.h> int main () { char s1[10], s2[10], s3[]= hello, *s4= hola ; strcpy(s1, hi ); strcpy(s2, hi ); } Value of the following expressions? sizeof(s1) 10 strcmp(s1,s2) 0 strlen(s1) 2 s1==s2 0 Point to different locations! strcmp(s1,s3) 4 strcmp(s1,s4) -6 (s1 > s3) e, f, g, h, i (s1 < s4) i, j, k,l, m, n, o 6/21/2017 CS61C Su18 - Lecture 3 27

Agenda Miscellaneous C Syntax Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 28

Pointer Arithmetic pointer ± number e.g. pointer + 1 adds 1 something to the address Compare what happens: (assume a at address 100) char *p; char a; int *p; int a; p = &a; printf( %u %u\n,p,p+1); 100 101 100 104 Adds 1*sizeof(char) Adds 1*sizeof(int) Pointer arithmetic should be used cautiously 6/21/2017 CS61C Su18 - Lecture 3 29

Pointer Arithmetic A pointer is just a memory address, so we can add to/subtract from it to move through an array p+1 correctly increments p by sizeof(*p) i.e. moves pointer to the next array element What about an array of structs? Struct declaration tells C the size to use, so handled like basic types 6/21/2017 CS61C Su18 - Lecture 3 30

Pointer Arithmetic What is valid pointer arithmetic? Add an integer to a pointer Subtract 2 pointers (in the same array) Compare pointers (<, <=, ==,!=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing) Everything else is illegal since it makes no sense: Adding two pointers Multiplying pointers Subtract pointer from integer 6/21/2017 CS61C Su18 - Lecture 3 31

Question: The first printf outputs 100 5 5 10. What will the next two printf output? int main(void){ int A[] = {5,10}; int *p = A; 5 10 A[0] A[1] p } printf( %u %d %d %d\n, p, *p, A[0], A[1]); p = p + 1; printf( %u %d %d %d\n, p, *p, A[0], A[1]); *p = *p + 1; printf( %u %d %d %d\n, p, *p, A[0], A[1]); (A) (B) (C) (D) 101 10 5 10 then 101 11 5 11 104 10 5 10 then 104 11 5 11 100 6 6 10 then 101 6 6 10 100 6 6 10 then 104 6 6 10 32

(REVIEW) Operator Precedence For precedence/order of execution, see Table 2-1 on p. 53 of K&R Prefix (++p) takes effect immediately Postfix/Suffix (p++) takes effect last int main () { int x = 1; int y = ++x; // y = 2, x = 2 x--; int z = x++; // z = 1, x = 2 return 0; } 6/21/2017 CS61C Su18 - Lecture 3 33

Increment and Dereference When multiple prefixal operators are present, they are applied from right to left *--p decrements p, returns val at that addr -- binds to p before * and takes effect first ++*p increments *p and returns that val * binds first (get val), then increment immediately 6/21/2017 CS61C Su18 - Lecture 3 34

Increment and Dereference Postfixal in/decrement operators have precedence over prefixal operators (e.g. *) BUT the in/decrementation takes effect last because it is a postfix. The front of expression is returned. *p++ returns *p, then increments p ++ binds to p before *, but takes effect last Equivalent C code: char *p = hi ; // assume p has value 40 char c = *p++; // c = h, p = 41 c = *p; // c = i 6/21/2017 CS61C Su18 - Lecture 3 35

Increment and Dereference Postfixal in/decrement operators have precedence over prefixal operators (e.g. *) BUT the in/decrementation takes effect last because it is a postfix. The front of expression is returned. (*p)++ returns *p, then increments in mem Post-increment happens last Equivalent C code: char *p = bye ; // assume p has value 40 char c = (*p)++; // c = b, p = 40 c = *p; // c = c because b +1 = c 6/21/2017 CS61C Su18 - Lecture 3 36

Question: What does this function do when called? void foo(char *s, char *t) { while (*s) s++; while (*s++ = *t++) ; } (A) (B) (C) (D) Always throws an error Changes characters in string t to the next character in the string s Copies a string at address t to the string at address s Appends the string at address t to the end of the string at address s 37

Get To Know Your Staff Sukrit Suvansh Bribe Food Twix Chocolate without nuts Biggest Fear Darkness Spiders Unpopular Opinion EDM music is bad Didn't like Breaking Bad Café Getaway TeaOne Cory Strada 6/21/2017 CS61C Su18 - Lecture 3 38

Agenda C Operators Arrays Administrivia Strings More Pointers Pointer Arithmetic Pointer Misc 6/21/2017 CS61C Su18 - Lecture 3 39

Pointers and Allocation When you declare a pointer (e.g. int *ptr;), it doesn t actually point to anything yet It points somewhere (garbage; don t know where) Dereferencing will usually cause an error Option 1: Point to something that already exists int *ptr,var; var = 5; ptr = &var1; var has space implicitly allocated for it (declaration) Option 2: Allocate room in memory for new thing to point to (next lecture) 6/21/2017 CS61C Su18 - Lecture 3 40

Pointers and Structures Variable declarations: struct Point { int x; int y; struct Point *p; }; struct Point pt1; struct Point pt2; struct Point *ptaddr; Valid operations: /* dot notation */ int h = pt1.x; pt2.y = pt1.y; Cannot contain an instance of itself, but can point to one /* arrow notation */ int h = ptaddr->x; int h = (*ptaddr).x; /* This works too */ pt1 = pt2; Copies contents 6/21/2017 CS61C Su18 - Lecture 3 41

Pointers to Pointers Pointer to a pointer, declared as **h Example: void IncrementPtr(int **h) { *h = *h + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); printf( *q = %d\n, *q); A q q 50 60 70 *q: 60 6/21/2017 CS61C Su18 - Lecture 3 42

Question: Struct and Pointer Practice Assuming everything is properly initialized, what do the following expressions evaluate to? struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 1) &p 2) p->name 3) p[7]->next 4) *(*(p + 2)) 5) *(p[0]->next) 6) (*p)->next->name 43

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 1) &p: address (ptr to ptr to ptr) address of operator returns an address 44

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 2) p->name: invalid Attempt to access field of a pointer address 45

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 3) p[7]->next invalid Increment p into unknown memory, then dereference 46

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 4) *(*(p + 2)) data (struct node) Move along array, access pointer, then access struct 47

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 5) *(p[0]->next) data (struct node) This is tricky. p[0] = *(p + 0) is valid and accesses the array of pointers, where -> operator correctly accesses field of struct, and dereference leaves us at another struct. 48

Answers: Struct and Pointer Practice struct node { char *name; struct node *next; data }; invalid struct node *foo[5]; struct node **p = foo;... /* fill foo with initialized structs */ address 6) (*p)->next->name address (char array) next field points to struct, access name field, which is, itself, a pointer (string) 49

Answers: Struct and Pointer Practice 1) &p address (ptr to ptr to ptr) address of operator returns an address 2) p->name invalid Attempt to access field of a pointer 3) p[7]->next invalid Increment p into unknown memory, then dereference 4) *(*(p + 2)) data (struct node) Move along array, access pointer, then access struct 5) *(p[0]->next) data (struct node) This is tricky. p[0] = *(p + 0) is valid and accesses the array of pointers, where -> operator correctly accesses field of struct, and dereference leaves us at another struct. 6) (*p)->next->name address (char array) next field points to struct, access name field, which is, itself, a pointer (string) 50

Summary Pointers and array variables are very similar Can use pointer or array syntax to index into arrays Strings are null-terminated arrays of characters Pointer arithmetic moves the pointer by the size of the thing it s pointing to Pointers are the source of many bugs in C, so handle with care 6/21/2017 CS61C Su18 - Lecture 3 51

Pointer Arithmetic to Copy Memory We can use pointer arithmetic to walk through memory: void copy(int *from, int *to, int n) { int i; for (i=0; i<n; i++) { *to++ = *from++; } } We have to pass the size(n) to copy 6/21/2017 CS61C Su18 - Lecture 3 52