{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter9

# Chapter9 - An Introduction to Pointers Engineering Problem...

This preview shows pages 1–4. Sign up to view the full content.

11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction to Pointers An Introduction to Pointers ± Addresses and Pointers ± Pointers to Array Elements ± Dynamic Memory Allocation ± Data Structures and the STL. 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 2 ADDRESSES AND POINTERS address operator pointer assignment pointer arithmetic 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 3 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 4 Addresses and Pointers ± A pointer is an object that holds the memory address of another object. ± If a variable p contains the address of another variable q , then p is said to point to q . ± If q is a variable at location 100 in memory, then p would have the value 100 ( q ’s address). Memory snapshot: q p

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

View Full Document
11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 5 Address Operator ± The operator & is called the address operator. ± When the & operator is applied to an object, the result is the address of the object. ± Example: int x=75; cout << "x is " << x; cout << "\nthe addres of x is " << &x; 75 x [0x7fff8164] OUTPUT: x is 75 the address of x is 0x7fff8164 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 6 Pointer Assignment ± Pointer types are declared using the pointer operator * , also called the dereferencing operator. ± Syntax - type *variable_name, *variable_name; type* variable_name; ± When declaring more than one pointer variable, the * operator must precede each identifier. 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 7 Example int *iPtr; double* dPtr; ± The variable iPtr is declared to be of type pointer to int. ± The variable dPtr is declared to be of type pointer to double. ± Neither variable in this example has been initialized. iPtr dPtr ? ? 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 8 Example int *iPtr, i=6; char* s, str[] = "example"; double *dPtr, d=1.25; iPtr s dPtr 6 "example" 1.25 i str d
11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 9 Initialization and Assignment ± Pointer types may be initialized at the time they are declared. ± Pointer types may be assigned new values using the assignment operator. 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 10 Example – Initial Pointers to NULL int *iPtr=0; char *s=NULL; //predefined constant in iostream double *dPtr=NULL; iPtr s dPtr NULL pointer ± there is a literal value that can be assigned to any pointer ± the literal value is NULL or 0 ± in this context it is known as the null address and does not point to anything which can be referenced. ± trying to dereference the null address, results in a terminal error 11/20/2008 Engineering Problem Solving with C++, second edition, J. Ingber 12 Assignment ± The assignment operator ( = ) is defined for pointers of the same base type.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern