2008SpCS61C-L04-ddg-c2-6up

2008SpCS61C-L04-ddg-c2-6up - inst.eecs.berkeley.edu/~cs61c...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 – Introduction to C (pt 2) 2007-01-30 bspace.berkeley.edu Forward your email! Voting machine usability In a study of electronic voting machines, researchers found that people made errors 3% of the time on simple tasks, but 15% of the time on complicated tasks, such as switching their vote to another candidate! technologyreview.com/Infotech/20122/ Must-see talk Thu 4-5pm @ Sibley by Turing Award winner Fran Allen: “The Challenge of Multi-Cores: Think Sequential, Run Parallel” CS61C L04 Introduction to C (pt 2) (3) Garcia, Spring 2008 © UCB More C Pointer Dangers Declaring a pointer just allocates space to hold the pointer – it does not allocate something to be pointed to! Local variables in C are not initialized , they may contain anything. What does the following code do? void f() { int *ptr; *ptr = 5; } CS61C L04 Introduction to C (pt 2) (4) Garcia, Spring 2008 © UCB Arrays (1/5) Declaration : int ar[2]; declares a 2-element integer array. An array is really just a block of memory. int ar[] = {795, 635}; declares and fills a 2-elt integer array. Accessing elements : ar[num] returns the num th element. CS61C L04 Introduction to C (pt 2) (5) Garcia, Spring 2008 © UCB Arrays (2/5) Arrays are (almost) identical to pointers char *string and char string[] are nearly identical declarations They differ in very subtle ways: incrementing, declaration of filled arrays Key Concept : An array variable is a “pointer” to the first element. CS61C L04 Introduction to C (pt 2) (6) Garcia, Spring 2008 © UCB Arrays (3/5) Consequences: ar is an array variable but looks like a pointer in many respects (though not all) ar[0] is the same as *ar ar[2] is the same as *(ar+2) We can use pointer arithmetic to access arrays more conveniently. Declared arrays are only allocated while the scope is valid char *foo() { char string[32]; . ..; return string; } is incorrect CS61C L04 Introduction to C (pt 2) (7) Garcia, Spring 2008 © UCB Arrays (4/5) Array size n ; want to access from 0 to n-1 , so you should use counter AND utilize a constant for declaration & incr Wrong int i, ar[10]; for(i = 0; i < 10; i++){ . .. } Right #define ARRAY_SIZE 10 int i, a[ARRAY_SIZE]; for(i = 0; i < ARRAY_SIZE; i++){ . .. } Why? SINGLE SOURCE OF TRUTH You ʼ re utilizing indirection and avoiding maintaining two copies of the number 10
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS61C L04 Introduction to C (pt 2) (8) Garcia, Spring 2008 © UCB Arrays (5/5) 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; be careful! (You ʼ ll learn how to debug these in lab…) CS61C L04 Introduction to C (pt 2) (9) Garcia, Spring 2008 © UCB Pointer Arithmetic (1/2) Since a pointer is just a mem address, we can add to it to traverse an array.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

2008SpCS61C-L04-ddg-c2-6up - inst.eecs.berkeley.edu/~cs61c...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online