L04 - CS 61C L04 C Structs (1) Wawrzynek Fall 2007 UCB...

Info iconThis preview shows pages 1–5. 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

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: CS 61C L04 C Structs (1) Wawrzynek Fall 2007 UCB 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Structs & Memory Management CS 61C L04 C Structs (2) Wawrzynek Fall 2007 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. #include <string.h> CS 61C L04 C Structs (3) Wawrzynek Fall 2007 UCB Pointers (1/4) Sometimes you want to have a procedure increment a variable? What gets printed? void AddOne(int x) { x = x + 1; } int y = 5; AddOne( y); printf(y = %d\n, y); y = 5 review CS 61C L04 C Structs (4) Wawrzynek Fall 2007 UCB Pointers (2/4) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int * p) { * p = * p + 1; } int y = 5; AddOne( & y); printf(y = %d\n, y); y = 6 CS 61C L04 C Structs (3) Wawrzynek Fall 2007 UCB Pointers (1/4) Sometimes you want to have a procedure increment a variable? What gets printed? void AddOne(int x) { x = x + 1; } int y = 5; AddOne( y); printf(y = %d\n, y); y = 5 review CS 61C L04 C Structs (4) Wawrzynek Fall 2007 UCB Pointers (2/4) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int * p) { * p = * p + 1; } int y = 5; AddOne( & y); printf(y = %d\n, y); y = 6 review CS 61C L04 C Structs (5) Wawrzynek Fall 2007 UCB Pointers (3/4) But what if what you want changed is a pointer ? What gets printed? void IncrementPtr(int *p) { p = p + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( q); printf(*q = %d\n, *q); *q = 50 50 60 70 A q CS 61C L04 C Structs (6) Wawrzynek Fall 2007 UCB Pointers (4/4) Solution! Pass a pointer to a pointer , declared as **h Now what gets printed? void IncrementPtr(int **h ) { *h = *h + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( & q); printf(*q = %d\n, *q); *q = 60 50 60 70 A q q CS 61C L04 C Structs (7) Wawrzynek Fall 2007 UCB int main(void){ int A = {5,10}; int *p = A; printf(%u %d %d %d\n, p , *p , A[0] , A[1] ); p = p + 1; printf(%u %d %d %d\n, p , *p , A[0] , A[1] ); *p = *p + 1; printf(%u %d %d %d\n,...
View Full Document

This note was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 13

L04 - CS 61C L04 C Structs (1) Wawrzynek Fall 2007 UCB...

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

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