2008SpCS61C-L04-ddg-c2

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

Info iconThis preview shows pages 1–9. 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”
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) (2) Garcia, Spring 2008 © UCB Review All declarations go at the beginning of each function except if you use C99 . Only 0 and NULL evaluate to FALSE. All data is in memory. Each memory location has an address to use to refer to it and a value stored in it. A pointer is a C version of the address. * “follows” a pointer to its value & gets the address of a value
Background image of page 2
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; }
Background image of page 3

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) (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.
Background image of page 4
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.
Background image of page 5

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) (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
Background image of page 6
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 7

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!
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

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

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

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