Lecture02-Introduction2 - Introduction/Revision Part II...

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

View Full Document Right Arrow Icon
Introduction/Revision Part II Computer Programming II 1 Lecture 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
Learning Objectives: Ø To define the basic concepts of pointers in C/C++, their declaration and use. Ø To illustrate the applications of pointer variables and pointer operators . Ø To understand the relationship between pointers and arrays . Ø To understand what is pointer arithmetic . Ø To understand how to allocate memory dynamically during runtime. Ø To understand the difference between static array and dynamic array. Computer Programming II 2
Background image of page 2
Computer Programming II 3 1432 Address of a 3 a [ int ] int a = 3; int * p = a; - If we want to store an integer, we store it in a integer data type called 'int'. - If we want to store an address , we must use pointer (a data type for storing address). 4324 Address of p 1432 p [ int* ] p stores address of int. we get. .. Basics of Pointers Memory Pointers are essential in: - Dynamic array (size of array is known only during runtime, not during compile time) - Avoiding creating a cloned copy of existing object (save memory, simpler maintainance) - Dynamic polymorphism (Chapter 10-11)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Computer Programming II 4 1432 3 a [ int ] int a = 3; int* p = a; 4324 1432 p [ int* ] The ampersand ' ' is called address operator which returns the address of variable 'a'. 'p' stores the address of variable 'a' We now say that the value of variable 'p' is the address of variable 'a' . Since 'p' is itself a variable, it has its own address too. In our example above: BUT &p has a value of 4324 Basics of Pointers
Background image of page 4
Computer Programming II 5 To access the value pointed by p, we use *p . The symbol ' * ' is called indirection operator or dereferencing operator . int a = 3; int b = a; // b and a are 2 separate integers int c = a; // c references to a int * p = a; // p points to a cout << a <<" "<< b <<" "<< c <<" "<< *p << endl; b = 4; // Why a, c and *p do not become 4? cout << a <<" "<< b <<" "<< c <<" "<< *p << endl; *p = 7; // Why a and c become 7 too? cout << a <<" "<< b <<" "<< c <<" "<< *p << endl; a = 9; // Why c and *p become 9 too? cout << a <<" "<< b <<" "<< c <<" "<< *p << endl; c = 1; // Why a and *p become 1 too? cout << a <<" "<< b <<" "<< c <<" "<< *p << endl; 3 3 3 3 3 4 3 3 7 4 7 7 9 4 9 9 1 4 1 1 output: How to access the value pointed to by pointer 1432 a [ int ], c 1432 p [ int* ] 1436 b [ int ]
Background image of page 5

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

View Full DocumentRight Arrow Icon
int main() { int x = 10, y = 20; int* p1 = &x; // p1 points to x int* p2 = &y; // p2 points to y cout << x << " " << y << " " << *p1 << " " << *p2 << endl; (*p1)++; // equals to x++; *p2 += 4; // equals to y+=4; cout << x << " " << y << " " << *p1 << " " << *p2 << endl; p1 = p2; // p1 now also points to y cout << x << " " << y << " " << *p1 << " " << *p2 << endl; p2 = &x; // p2 points to x cout << x << " " << y << " " << *p1 << " " << *p2 << endl; } Computer Programming II 6 More example: Output: 10 20 10 20 11 24 11 24 11 24 24 24 11 24 24 11 Must have bracket, without them it means *(p1++)
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 30

Lecture02-Introduction2 - Introduction/Revision Part II...

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

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