pointer. lvalues in c++, any expression that refers to an internal memory location is called an...

22
Pointer

Upload: janel-hall

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Memory and Data Representation Every lvalue is stored somewhere in memory and therefore has an address Once allocated, the address of an lvalue never changes, even though its contents may change Depending on type of data, different lvalues require different amounts of memory The address of an lvalue is itself data that can be manipulated and stored in memory

TRANSCRIPT

Page 1: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointer

Page 2: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

lvalues

• In C++, any expression that refers to an internal memory location is called an lvalue

• Appear on left side of assignment statement• e.g.

x = 1.0;intarray[2] = 17;

• Constants and expressions are not lvalues

Page 3: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Memory and Data Representation• Every lvalue is stored somewhere in memory

and therefore has an address• Once allocated, the address of an lvalue never

changes, even though its contents may change• Depending on type of data, different lvalues

require different amounts of memory• The address of an lvalue is itself data that can be

manipulated and stored in memory

Page 4: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointer Declaration

• A pointer is a variable storing the address of another variable

• Declaration:type *ptr;

• Example:int *p1, *p2;

• p1 and p2 are referred to as pointers-to-int, and may contain the addresses of integers

Page 5: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointer Operations

• C++ defines two operators that manipulate pointer values:

& address-of* value-pointed-to (dereference)

• Operand of & must be lvalue, and & returns the address of the lvalue

• Operand of * must be a pointer, and * returns the value pointed to by the pointer

Page 6: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Getting the Address of a Variable

• Each variable in program is stored at a unique address

• Use address operator & to get address of a variable:int num = -99;cout << &num; // prints address// in hexadecimal

Page 7: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointer Variables

• Definition:int *intptr;

• Read as:“intptr can hold the address of an int”

• Spacing in definition does not matter:int * intptr; // same as aboveint* intptr; // same as above

Page 8: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointer Variables

• Assigning an address to a pointer variable:int *intptr;intptr = &num;

• Memory layout:num intptr25 0x4a00

address of num: 0x4a00

Page 9: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g
Page 10: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

The Indirection Operator

• The indirection operator (*) dereferences a pointer.

• It allows you to access the item that the pointer points to.

int x = 25;int *intptr = &x;cout << *intptr << endl;

This prints 25.

Page 11: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g
Page 12: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g
Page 13: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Special pointer NULL

• NULL is the value of a pointer that does not currently point to any data (usually value 0)

• Can not dereference a NULL pointer• Exists for applications

infile.open(”file.txt”);if (infile == NULL)

printf(“File not open”);

Page 14: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Pointers as Function Parameters• A pointer can be a parameter• Works like reference variable to allow change to

argument from within function• Requires:

1) asterisk * on parameter in prototype and headingvoid getNum(int *ptr); // ptr is pointer to an int 2) asterisk * in body to dereference the pointercin >> *ptr;

3) address as argument to the functiongetNum(&num); // pass address of num to getNum

Page 15: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Example

void swap(int *x, int *y){ int temp;

temp = *x;*x = *y;*y = temp;

}

int num1 = 2, num2 = -3;swap(&num1, &num2);

Page 16: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

(Program Continues)

Page 17: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g
Page 18: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Another Example

• To modify value of argument, use pointervoid SetToZero(int *ip){

*ip = 0;}

which makes “ip” a pointer to the location of the value of the argument of the call

SetToZero(&x);

Page 19: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Advantage of use of & operator

• When calling a function usingfunc(x);

you know the value of the variable x will not change, whereas calling a function usingfunc(&x);

is allowed to change the value of x• Easier to predict effects of function call

Page 20: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

SwapInteger function• When sorting arrays, we wanted to use

SwapIntegers(array[low], array[high]);

• Using pointers allows the following:void SwapIntegers(int *p1, int *p2){

int temp;temp = *p1;*p1 = *p2;*p2 = temp;

}

and SwapIntegers(&array[low], &array[high]);

Page 21: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Returning Multiple Results• Convert time (minutes) to hours and minutes

void ConvertTime(int time, int *hours, int *mins)

{*hours = time / 60;*mins = time % 60;

}

• Call with ConvertTime(t, &h, &m);

Page 22: Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g

Or Use Two functionsint Hours(int time){return (time / 60);

}

int Minutes(int time){return (time % 60);

}