L04-ac-6up - CS61C L4 C Pointers (1) Chae, Summer 2008 UCB...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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. 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 Flled arrays int *int_array = {1,2,3}; // ILLEGAL Key Difference : An array variable is a constant pointer to the Frst element. 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]) 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 . 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 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....
View Full Document

This note was uploaded on 12/06/2009 for the course CS cs61c taught by Professor Cory during the Summer '08 term at Berkeley College.

Page1 / 6

L04-ac-6up - CS61C L4 C Pointers (1) Chae, Summer 2008 UCB...

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