CS 61c: Great Ideas in Computer Architecture

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

ECE 15B COMPUTER ORGANIZATION

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

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

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

CS61C : Machine Structures

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

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

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

More C Pointer Dangers

CS61C : Machine Structures

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

CS61C : Machine Structures

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

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

CS61C : Machine Structures

CS61C : Machine Structures

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

CS61C : Machine Structures

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

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

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

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

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

Lecture 2: C Programm

Arrays and Memory Management

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. Lecture 3: Pointers. Bernhard Boser & Randy Katz

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

Intermediate Programming, Spring 2017*

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

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

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

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

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

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

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

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

Multidimension array, array of strings

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

Arrays and Pointers in C. Alan L. Cox

Memory, Arrays & Pointers

Lectures 5-6: Introduction to C

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

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

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

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

Kurt Schmidt. October 30, 2018

Pointers in C/C++ 1 Memory Addresses 2

CS 61c: Great Ideas in Computer Architecture

Homework #3 CS2255 Fall 2012

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

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

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

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

C++ for Java Programmers

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

Arrays and Pointers (part 1)

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

Character Strings. String-copy Example

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

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

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

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

Systems Programming and Computer Architecture ( )

Lectures 5-6: Introduction to C

CS 61c: Great Ideas in Computer Architecture

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 04 Introduction to pointers

Arrays and Pointers (part 1)

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

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

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

Lectures 13 & 14. memory management

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

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

CS 11 C track: lecture 5

PRINCIPLES OF OPERATING SYSTEMS

REFERENCES, POINTERS AND STRUCTS

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

CS 241 Data Organization Pointers and Arrays

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

A brief introduction to C programming for Java programmers

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

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

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

Array Initialization

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

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

Transcription:

Arrays, Strings, and Some More Pointers June 24, 2014

Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address vs. Value Allows for efficient and powerful code, but error prone C functions are pass by value Passing pointers circumvents this

Question: What is the result of executing the following code? # include <stdio.h> int main () { int *p; *p = 5; printf ("%d\n", *p); } (blue) Prints 5 (green) Prints garbage (purple) Guaranteed to crash (yellow) Probably crashes

Question: What is the result of executing the following code? # include <stdio.h> int main () { int *p; *p = 5; printf ("%d\n", *p); } (blue) Prints 5 (green) Prints garbage (purple) Guaranteed to crash (yellow) Probably crashes

Great Idea #1: Levels of Representation/Interpretation High Level Language Program (e.g.c) Compiler Assembly Language Program (e.g. MIPS) Assembler Machine Language Program temp = v[k]; v[k] = v[k+1] v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) 1000 1100 0100 1000 0000 0000 0000 0000 1000 1100 0100 1001 0000 0000 0000 0100 1010 1100 0100 1001 0000 0000 0000 0000 1010 1100 0100 1000 0000 0000 0000 0100 Machine Interpretation Hardware Architecture Description (e.g. block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams)

Outline Miscellaneous C Syntax C quirks Arrays Basics Relation to Pointers Administrivia Strings Working with Strings More Pointers Pointer Arithmetic Pointer Miscellaneous

Expansion on Struct Declarations Structure definition: Does NOT declare a variable Variable type is struct name struct name bob, *pn, name_arr[3]; Joint struct definition and typedef possible struct name { /* fields */ }; struct nm { /* fields */ }; typedef struct nm name ; name n1; Equiv. typedef struct nm { /* fields */ } name ; name n1;

C quirks Assignment and Equality One of the most common errors for beginning C programmers (a = b) is an assignment (a == b) is an equality test Comparisons will use assigned values Assignments return the value assigned if (a = b) {... } is legal, but probably not what you meant A trick for avoiding this mistake Put the constant on the left when comparing if (3 == a) {... } Correct if (3 = a) {... } Compilation Error

C quirks Operator Precedence Operators Associativity () [] ->. left to right! ++ -- + - * (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right ==!= left to right & left to right ˆ left to right left to right && left to right left to right?: right to left = += -= *= %= &= ˆ= = <<= >>= right to left, left to right

C quirks 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), rather than (x & 1) == 0 Pre-increment (++p) takes effect first Post-increment (p++) takes effect last

C quirks Increment and Dereference Dereference operator (*) and (in/de)crement operators are the same level of precedence and are applied from right to left *p++ returns *p, then increments p ++ binds to p before *, but takes effect last

C quirks Increment and Dereference Dereference operator (*) and (in/de)crement operators are the same level of precedence and are applied from right to left *p++ returns *p, then increments p ++ binds to p before *, but takes effect last *--p decrements p, returns whatever is at that address -- binds to p before *, and takes effect first

C quirks Increment and Dereference Dereference operator (*) and (in/de)crement operators are the same level of precedence and are applied from right to left *p++ returns *p, then increments p ++ binds to p before *, but takes effect last *--p decrements p, returns whatever is at that address -- binds to p before *, and takes effect first ++*p increments *p, then returns that value * binds to ++ before *

C quirks Increment and Dereference Dereference operator (*) and (in/de)crement operators are the same level of precedence and are applied from right to left *p++ returns *p, then increments p ++ binds to p before *, but takes effect last *--p decrements p, returns whatever is at that address -- binds to p before *, and takes effect first ++*p increments *p, then returns that value * binds to ++ before * (*p)++ returns *p, then increments in memory * binds to p before ++, and takes effect first

C quirks Question: What is the output of the following code? char blocks [] = { 6, 1, c }; char * ptr = blocks, temp ; temp = *++ ptr ; printf ("1: %c\n", tmp ); temp = * ptr ++; printf ("2: %c\n", tmp ); 1 2 blue 7 8 green 7 1 purple 1 1 yellow 1 C

C quirks Question: What is the output of the following code? char blocks [] = { 6, 1, c }; char * ptr = blocks, temp ; temp = *++ ptr ; printf ("1: %c\n", tmp ); temp = * ptr ++; printf ("2: %c\n", tmp ); 1 2 blue 7 8 green 7 1 purple 1 1 yellow 1 C

Outline Miscellaneous C Syntax C quirks Arrays Basics Relation to Pointers Administrivia Strings Working with Strings More Pointers Pointer Arithmetic Pointer Miscellaneous

Basics Array Syntax Declaration: int ar[2]; declares a 2-element array of integers int ar[] = {795, 635}; declares and initialized a 2-element integer array Accessing elements: ar[num] returns the num-th element of ar Zero-indexed

Basics Array Pitfalls Pitfall: An array in C does not know its own length, and its bounds are not checked! We can accidentally access elements past the end of an array Not even guaranteed to fail when that happens! We must pass the array and its size (or use sentinel values, more on that later) to any procedure manipulating it. Mistakes with array bounds manifest as segmentation faults and bus errors Very difficult to find, best to be careful when coding to avoid them as much as possible.

Basics Accessing Arrays Array size n: can access entries in the range [0,n-1] Use a variable or constant for declaration of length /* Blegh, magic numbers! */ int i, arr [10]; for (i = 0; i < 10; i ++) {... }

Basics Accessing Arrays Array size n: can access entries in the range [0,n-1] Use a variable or constant for declaration of length /* Blegh, magic numbers! */ int i, arr [10]; for (i = 0; i < 10; i ++) {... } /* Single source of truth. Much better. */ int ARRAY_SIZE = 10; int i, arr [ ARRAY_SIZE ]; for (i = 0; i < ARRAY_SIZE ; i ++) {... }

Relation to Pointers 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 0-th element ar[0] same as *ar and ar[2] same as *(ar + 2) We can use pointer arithmetic to conveniently access arrays An array variable is read-only (no assignment) cannot use ar = anything

Relation to Pointers Array and Pointer Example Remember: ar[i] is treated as *(ar + i) Three different ways of zeroing an array 1. for (i = 0; i < SIZE; i++) ar[i] = 0;

Relation to Pointers Array and Pointer Example Remember: ar[i] is treated as *(ar + i) Three different ways of zeroing an array 1. for (i = 0; i < SIZE; i++) ar[i] = 0; 2. for (i = 0; i < SIZE; i++) *(ar + i) = 0;

Relation to Pointers Array and Pointer Example Remember: ar[i] is treated as *(ar + i) Three different ways of zeroing an array 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;

Relation to Pointers Array and Pointer Example Remember: ar[i] is treated as *(ar + i) Three different ways of zeroing an array 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 ll cover in more detail shortly

Relation to Pointers Arrays Stored Differently Than Pointers void foo () { int *p, a[4], x; p = &x } *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... p x a 24

Relation to Pointers Arrays Stored Differently Than Pointers void foo () { int *p, a[4], x; p = &x } *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... a 24 p x

Relation to Pointers Arrays Stored Differently Than Pointers void foo () { int *p, a[4], x; p = &x } *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 1... a 24 p x

Relation to Pointers Arrays Stored Differently Than Pointers void foo () { int *p, a[4], x; p = &x } *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 a 24 *p:1, p:40, &p:20

Relation to Pointers Arrays Stored Differently Than Pointers void foo () { int *p, a[4], x; p = &x } *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 a 24 *p:1, p:40, &p:20 *a:2, a:24, &a:24

Relation to Pointers Arrays and Functions Declared arrays only allocated while the scope is valid: /** This function is EVIL. */ char * foo () { char string [32];...; return string ; } An array is passed to a function as a pointer int foo ( int ar [], // Actually int *ar unsigned size ) {... ar[ size - 1]... }

Relation to Pointers Arrays and Functions Array size gets lost when passed to a function What prints in the following code: int foo ( int array [], unsigned size ) {... printf ("%d\n", sizeof ( array )); } int main ( void ) { int a [10], b [5];... foo (a, 10)... printf ("%d\n", sizeof (a )); }

Relation to Pointers Arrays and Functions Array size gets lost when passed to a function What prints in the following code: int foo ( int array [], unsigned size ) {... printf ("%d\n", sizeof ( array )); } sizeof(int *) int main ( void ) { int a [10], b [5];... foo (a, 10)... printf ("%d\n", sizeof (a )); }

Relation to Pointers Arrays and Functions Array size gets lost when passed to a function What prints in the following code: int foo ( int array [], unsigned size ) {... printf ("%d\n", sizeof ( array )); } sizeof(int *) int main ( void ) { int a [10], b [5];... foo (a, 10)... printf ("%d\n", sizeof (a )); } 10*sizeof(int)

Outline Miscellaneous C Syntax C quirks Arrays Basics Relation to Pointers Administrivia Strings Working with Strings More Pointers Pointer Arithmetic Pointer Miscellaneous

Administrivia Lab 2 tomorrow HW1 due this Sunday

Outline Miscellaneous C Syntax C quirks Arrays Basics Relation to Pointers Administrivia Strings Working with Strings More Pointers Pointer Arithmetic Pointer Miscellaneous

Working with Strings C Strings A string in C is just an array of characters char string [] = " abc "; // 4 bytes needed How do you tell how long a string is? Last character is followed by a null terminator ( \0 == 0) Need extra space in array for null terminator int strlen ( char s []) { int n = 0; while (s[n]) n ++; return n; }

Working with Strings C String Libraries Accessible with #include <string.h> int strlen(char *string); Returns the length of string (excluding the null terminator) int strcmp(char *str1, char *str2); Compares str1 and str according to a lexical ordering 0 if str1 is identical to str2 (how different from str1 == str2?) char *strcpy(char *dst, char *src); Copies the contents of src to the memory pointed to by dst. Caller must ensure that dst is large enough to hold the copied data Why not dst = src?

Working with Strings String Examples # include <stdio.h> # include <string.h> int main () { char s1 [10], s2 [10]; char s3 []= " hello ", *s4=" hola "; strcpy (s1,"hi"); strcpy (s2,"hi"); } Values of the following expressions? 1. sizeof(s1) 2. strlen(s1) 3. s1 == s2 4. strcmp(s1,s2) 5. strcmp(s1,s3) 6. strcmp(s1,s4)

Working with Strings Question: What does this function do when called? void foo ( char *s, char *t) { while (*s) s ++; while (*s++ = *t ++); } (blue) Always throws an error (green) changes characters in string t to the next character in the string s (purple) Copies a string at address t to the string at address s (yellow) Appends the string at address t to the end of the string at address s

Working with Strings Question: What does this function do when called? void foo ( char *s, char *t) { while (*s) s ++; while (*s++ = *t ++); } (blue) Always throws an error (green) changes characters in string t to the next character in the string s (purple) Copies a string at address t to the string at address s (yellow) Appends the string at address t to the end of the string at address s

Outline Miscellaneous C Syntax C quirks Arrays Basics Relation to Pointers Administrivia Strings Working with Strings More Pointers Pointer Arithmetic Pointer Miscellaneous

Pointer Arithmetic Pointer Arithmetic pointer ± number e.g. p + 1 adds 1 something to p 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 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 large structs? Struct declaration tells C the size to use, so handled like basic types

Pointer Arithmetic Pointer Arithmetic What constitutes valid pointer arithmetic? Add an integer to a pointer Substract 2 pointers (in the same array) Compare pointers (<, <=, ==,!=,>, >=) Compare pointer to NULL Everything else is illegal since it makes no sense: Adding two pointers Multiplying pointers Subtracting a pointer from an integer

Pointer Arithmetic 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 += 1) { *to ++ = * from ++; } } Note: we have to pass the size (n) to copy

Pointer Arithmetic Question: The first printf outputs 100 5 5 10. What will the next two printfs output? int main ( void ){ int A[] = {5,10}; int *p = A; 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]); } (blue) 101 10 5 10 then 101 11 5 11 (green) 104 10 5 10 then 104 11 5 11 (purple) 100 6 6 10 then 101 6 6 10 (yellow) 100 6 6 10 then 104 6 6 10

Pointer Arithmetic Question: The first printf outputs 100 5 5 10. What will the next two printfs output? int main ( void ){ int A[] = {5,10}; int *p = A; 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]); } (blue) 101 10 5 10 then 101 11 5 11 (green) 104 10 5 10 then 104 11 5 11 (purple) 100 6 6 10 then 101 6 6 10 (yellow) 100 6 6 10 then 104 6 6 10

Pointer Arithmetic Delayed Icebreaker/Technology Break Here are the rules You say your name, your question for me, and your answer to that question. Then I answer your question and the next person goes.

Pointer Arithmetic Delayed Icebreaker/Technology Break Here are the rules You say your name, your question for me, and your answer to that question. Then I answer your question and the next person goes. Who s first?

Pointer Miscellaneous Pointers and Allocation When you declare a pointer (e.g. int *ptr;), it doesn t actually point to anything yet I points somewhere, but we don t know where Dereferencing will usually cause an error Option 1: Point to something that already exists int *ptr, var; var = 5; ptr = &var; var has space implicitly allocated for it (declaration) Option 2: Allocate room in memory for something new to point to (next lecture)

Pointer Miscellaneous Pointers and Structures Variable declarations: struct point { int x; int y; /* As close to containing * an instance of ourself * as is possible. */ struct point * p; }; struct Point pt1 ; struct Point pt2 ; struct Point * ptaddr ; Some Valid operations: /* dot notation */ int h = pt1. x; pt2.y = pt1.y; /* arrow notation */ int h = ptaddr - >x; int h = (* ptaddr ). x; /* struct assignment. * Copies contents. */ pt1 = pt2 ;

Pointer Miscellaneous Handles A pointer to a pointer, declared as int **h (of course, doesn t have to be an int handle.) Example: void incr_ptr ( int **h) { *h = *h + 1; } A q int A [3] = {50, 60, 70}; int *q = A; incr_ptr (&q); printf ("*q = %d\n", *q); 50 60 70

Pointer Miscellaneous Handles A pointer to a pointer, declared as int **h (of course, doesn t have to be an int handle.) Example: void incr_ptr ( int **h) { *h = *h + 1; } A q int A [3] = {50, 60, 70}; int *q = A; incr_ptr (&q); printf ("*q = %d\n", *q); 50 60 70

Pointer Miscellaneous Handles A pointer to a pointer, declared as int **h (of course, doesn t have to be an int handle.) Example: void incr_ptr ( int **h) { *h = *h + 1; } A q int A [3] = {50, 60, 70}; int *q = A; incr_ptr (&q); printf ("*q = %d\n", *q); 50 60 70

Pointer Miscellaneous Handles A pointer to a pointer, declared as int **h (of course, doesn t have to be an int handle.) Example: void incr_ptr ( int **h) { *h = *h + 1; } A q int A [3] = {50, 60, 70}; int *q = A; incr_ptr (&q); printf ("*q = %d\n", *q); 50 60 70 *q = 60

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

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

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

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

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

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

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

Pointer Miscellaneous 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