{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–3. 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 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 filled arrays int *int_array = {1,2,3}; // ILLEGAL Key Difference : An array variable is a constant “pointer” to the first 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
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern