L04-ac - inst.eecs.berkeley.edu/~cs61c CS61C : Machine...

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

View Full Document Right Arrow Icon
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings, Arrays, & Malloc 2008-06-26
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 L4 C Pointers (2) Chae, Summer 2008 © UCB Review: C Strings A string in C is just an array of characters. char string[] = "abc"; Strings are ended by 0 byte (null terminator Value as int, 0 Value as char, '\0' Programmer manually manages memory for strings.
Background image of page 2
CS61C L4 C Pointers (3) Chae, Summer 2008 © UCB Review: Arrays vs. Pointers Arrays are (almost) identical to pointers char *str_ptr and char str_array[] are nearly identical They differ in very subtle ways: - Incrementing str_array++; // ILLEGAL - declaration of filled arrays int *int_array = {1,2,3}; // ILLEGAL Key Difference : An array variable is a constant “pointer” to the first element.
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 L4 C Pointers (4) Chae, Summer 2008 © UCB Review: Arrays vs. Pointers An array parameter can be declared as an array or a pointer; an array argument can be passed as a pointer. int strlen(char s[] ) { int n = 0; while (s[n] != 0) n++; return n; } int strlen(char *s ) { int n = 0; while (s[n] != 0) n++; return n; } Could be written: while (s[n])
Background image of page 4
CS61C L4 C Pointers (5) Chae, Summer 2008 © UCB Review: Pointer Arithmetic Since a pointer is just a mem address, we can add to it to traverse an array. p+1 returns a ptr to the next array elt. *p++ vs (*p)++ ? x = *p++ x = *p ; p = p + 1; x = (*p)++ x = *p ; *p = *p + 1; What if we have an array of large structs (objects)? C takes care of it: In reality, p+1 doesn’t add 1 to the memory address, it adds the size of the (type of) array 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 L4 C Pointers (6) Chae, Summer 2008 © UCB Pointer Arithmetic Summary x = *(p+1) ? x = *(p+1) ; x = *p+1 ? x = (*p) + 1 ; x = (*p)++ ? x = *p ; *p = *p + 1; x = *p++ ? (*p++) ? *(p)++ ? *(p++) ? x = *p ; p = p + 1; x = *++p ? p = p + 1 ; x = *p ; Lesson? Using anything but the standard *p++ , (*p)++ causes more problems than it solves! P. 53 is a precedence table if you ever have to deal with this
Background image of page 6
CS61C L4 C Pointers (7) Chae, Summer 2008 © UCB 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 ?) char * 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.
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 L4 C Pointers (8) Chae, Summer 2008 © UCB Pointers to pointers (1/4) Sometimes you want to have a procedure increment a variable?
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 / 32

L04-ac - inst.eecs.berkeley.edu/~cs61c CS61C : Machine...

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