cs31-lecture13

cs31-lecture13 - Monday,November22 PointerChallenge...

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

View Full Document Right Arrow Icon
Monday, November 22 nd Pointer Challenge Structures Classes (Object Oriented Programming) Add using arrays of structures, iterating through arrays of  structures using [], *(ptr+j) and *ptr/ptr++;
Background image of page 1

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

View Full DocumentRight Arrow Icon
What are the values in the  ptr  variable and  a   array after this program finishes. Pointer Review void main(void) { int *ptr; int a[5] = {0}; ptr = &a[1]; *ptr = 3; ptr[1] = 4; *(ptr-1) = 5; ptr += 3; *ptr = 10; // what are the values? } Assume array  a  is at address 3000 and  the  ptr  variable is at address 3100.
Background image of page 2
char *GetName(void) { char temp[7]; cout << "Enter your name: “; cin >> temp; return(temp); } void main(void) { char *ptr; ptr = GetName(); cout << “Hi “ << ptr; } Find the Bug! 00000000 00000001 00002000 00002001 00002002 00002003 00002004 00002005 00002006 00002007 00002008 00002009 00002010 00002011 00002012 00002013 00002104 ...         ptr temp                         Enter your name:  Ingy ‘I’ ‘n’ ‘g’ ‘y’ 0 2004 2004
Background image of page 3

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

View Full DocumentRight Arrow Icon
char *GetName(void) { char temp[7]; cout << "Enter your name: “; cin >> temp; return(temp); } void main(void) { char *ptr; ptr = GetName(); cout << “Hi “ << ptr; } Find the Bug! 00000000 00000001 00002000 00002001 00002002 00002003 00002004 00002005 00002006 00002007 00002008 00002009 00002010 00002011 00002012 00002013 00002104 ...         ptr Enter your name:  Ingy 2004 ‘I’ ‘q’ ‘$’ ‘(’ ‘R’ ‘Z’ ‘#’ ‘A’ ‘N’ ‘g’ ‘y’ ‘}’ ‘r’ Hi  #Angy}r… When our  GetName  function returns,  the  temp  variable  goes away and  the memory is re- used!
Background image of page 4
What’s Wrong With It? void swat(int *p) { *p = 5; } void main(void) { int *a; swat(a); cout << *a; }   00000000 00000001 00002000 00002001 00002002 00002003 00002004 00002005 00002006 00002007 00002008 00002009 00002010 00002011         a Martha Stewart Recipes? 459292 459292         p 459292 *p = 5 stores the value of 5 in location 459292… 5 (BOOM!)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Motivation for Structures Let’s say we want to write a program that keeps a list of students at  UCLA and lets us search through them.   We’ll want to maintain a  name , an  ID  and a  GPA  for each student. How can we do this?
Background image of page 6
Motivation for Structures const int  NUM_STUDENTS  = 50000; main() {     string name[ NUM_STUDENTS ];     int ID_number[ NUM_STUDENTS ];     float GPA[ NUM_STUDENTS ];     name[ 0 ] = “Carey Nachenberg”;     ID_number[ 0 ] = 113353948;     GPA[ 0 ] = 3.99;     name[ 1 ] = “David Smallberg”;     ID_number[ 1 ] = 112233445;     GPA[ 1 ] = 1.90;     . .. This is one way we can  solve this problem.   We can have a  separate  array  for each piece of  information on a student. But this is pretty  ugly …  
Background image of page 7

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

View Full DocumentRight Arrow Icon
Structures It would be much nicer if we could group all three pieces of information  into a  single unit for each student , rather than use  three separate arrays . Carey N. David S. Yutao H. Yuval T. 113353948 112233445 595823747 010284732 3.99 1.90 3.75 4.0 Name:  Carey N. ID:  113353948 GPA : 3.99 Name:  David S. ID:  112233445 GPA : 1.90 Name:  Yutao H. ID:  595823747 GPA : 3.75 Name:  Yuval T.
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 / 37

cs31-lecture13 - Monday,November22 PointerChallenge...

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