Chapter9 - An Introduction to Pointers Engineering Problem...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/23/2011 for the course ECE 224 taught by Professor Wangxin during the Spring '09 term at SUNY Stony Brook.

Page1 / 16

Chapter9 - An Introduction to Pointers Engineering Problem...

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

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