Download presentation
Presentation is loading. Please wait.
Published byRachel Jewel Flowers Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers
2
Midterm Survey Feedback Satisfied with Exam 1 format and difficulty level Exam 2 will follow the similar format and same level of difficulties 11/14 Wednesday, 9:00-9:55 am Exam 3 will also follow the similar format and same level of difficulties 12/10 Monday, 9:00-9:55 am; does not need 3 hour exam in the final week (No exam after 12/10) Ask you to focus on the labs and project Pace of class: a little bit slow Take care of students’ diversity The scheduled classes will be completed on time Post Lab on Monday afternoon, Due on next Tuesday More time to prepare the lab The difficulty levels of remaining Labs are gradually increased 12/16/2015 ECE 264: Lecture 22 2
3
Lecture outline Announcements / reminders Lab 6 is due 10/31 (new date) Note that project design is due 11/19 (Monday) In Lab 7, you will be playing with Microsoft Visio for UML design (Due 11/07, Wednesday) Today Pointers Basic usage Pointer arithmetic Common pointer problems Passing arguments by reference 12/16/2015 ECE 264: Lecture 22 3
4
Dynamic memory allocation Up until now, allocated memory statically Assumed we knew data size at compile time What if data size is input-dependent and unknown until run time? In C, dynamic memory allocation handled through malloc and free In C++, we use new and delete 12/16/2015 ECE 264: Lecture 22 4
5
Refresher on pointers Allocators ( malloc, new ) return pointer to allocated space Pointer: address of another object We implicitly use these when we pass function arguments by reference in C++ Can get address of existing object using & Can get value of existing pointer using * Pointer declaration: * Base type determines how reference is interpreted Be careful when declaring multiple pointers Be sure to initialize pointer before use 12/16/2015 ECE 264: Lecture 22 5
6
Pointer example int *iPtr, i=6; char* s, str[] = "example"; double *dPtr, d=1.25; 12/16/2015 ECE 264: Lecture 22 6 iPtr s dPtr 6 "example" 1.25 i str d
7
Pointer assignment The assignment operator (=) is defined for pointers of the same base type. The right operand of the assignment operator can be any expression that evaluates to the same type as the left operand. Example: int x, *xp, *ip; xp = &x; ip = xp; 12/16/2015 ECE 264: Lecture 22 7 x xp ip
8
Arrays and pointers Array name is a pointer to first array element Can use pointers and arrays interchangeably You can use [] to “index” a pointer Example: char myString[] = "This is a string"; char *str; str = myString; for(int i =0; str[i]; i++)//look for null cout << str[i]; What does this print? 12/16/2015 ECE 264: Lecture 22 8
9
Initialize the Pointer Both define the pointer student and initialize student to the address of the first element in section: int section[80]; int *student = section; Is equivalent to int section[80]; int *student = §ion[0]; The pointer string is initialized to point to the character a in the string "abcd". char *string = "abcd"; 12/16/2015 ECE 264: Lecture 22 9
10
Pointer arithmetic When using pointers/arrays interchangeably, can make use of pointer arithmetic Can’t change where array name points, but you can change pointer If p is a pointer, p++ means “point to next element” “Next element” determined by base type Can compare pointers p == NULL pointer points nowhere p == q p and q point to same location Example int num[4] = {1,2,3,4}, *p; p = num;//same as p = &num[0]; cout << *p <<endl; ++p; cout << *p; 12/16/2015 ECE 264: Lecture 22 10
11
Practice! 12/16/2015 ECE 264: Lecture 22 11 int q=6; int *iPtr = &q; cout << "iPtr is " << iPtr << endl; cout << "*iPtr is " << *iPtr << endl; cout << "++*iPtr, is " << ++*iPtr << endl; cout << "q is " << q << endl; cout << "iPtr is " << iPtr << endl; cout << "*iPtr++ is " << *iPtr++ << endl; cout << "iPtr is " << iPtr << endl; cout << "q is " << q << endl; Complete the output: iPtr is 0x7fff2f14
12
12/16/2015 ECE 264: Lecture 22 12 Result of Practice iPtr is 0x7fff2f14 *iPtr is 6 ++*iPtr is 7 q is 7 iPtr is 0x7fff2f14 *iPtr++ is 7 iPtr is 0x7fff2f18 q is 7
13
Practice #2 char myString[ ] = "This is a string"; char *strPtr; strPtr = myString; cout << *myString << endl; cout<<myString << endl; cout << *(myString + 1) << endl; strPtr++; cout << *++strPtr << endl; myString++;//not legal What does this print? 12/16/2015 ECE 264: Lecture 22 13 0xfff4c252 strPtr 0xfff4c252 myString Thisisastring\0
14
Practice #2 result 12/16/2015 ECE 264: Lecture 22 14 T This is a string h i
15
Common Pointer Problems Using uninitialized pointers int *iPtr; *iPtr = 100; iPtr has not been initialized. The value 100 will be assigned to some memory location. Which one determines the error. Incorrect/unintended syntax. 12/16/2015 ECE 264: Lecture 22 15
16
Example #include int main() { char *aString = "What happens here?"; int len=0; while(*aString++ != '\0') len++; std::cout << len << ": " << aString; return 0; } Does this compile? If not, why? If it does, what is the output? Explain 12/16/2015 ECE 264: Lecture 22 16
17
Final notes Next time Start dynamic allocation (time permitting) Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 12/16/2015 ECE 264: Lecture 22 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.