lecture13%2Dpost

Lecture13-post - Monday November 22 nd • Pointer Challenge • Structures • Classes(Object Oriented Programming What are the values in the ptr

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

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: Monday, November 22 nd • Pointer Challenge • Structures • Classes (Object Oriented Programming) 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. 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’ 2004 2004 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! 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!) 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? Motivation for Structures const int NUM_STUDENTS = 50000; main() { string name[ NUM_STUDENTS ]; int ID_number[ NUM_STUDENTS ]; float GPA[ NUM_STUDENTS ]; name[ ] = “Carey Nachenberg”; ID_number[ ] = 113353948; GPA[ ] = 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....
View Full Document

This note was uploaded on 04/10/2012 for the course CS 31 taught by Professor Melkanoff during the Winter '00 term at UCLA.

Page1 / 36

Lecture13-post - Monday November 22 nd • Pointer Challenge • Structures • Classes(Object Oriented Programming What are the values in the ptr

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

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