pointer. lvalues in c++, any expression that refers to an internal memory location is called an...
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 memoryTRANSCRIPT
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.
x = 1.0;intarray[2] = 17;
• Constants and expressions are not lvalues
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
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
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
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 << # // prints address// in hexadecimal
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
Pointer Variables
• Assigning an address to a pointer variable:int *intptr;intptr = #
• Memory layout:num intptr25 0x4a00
address of num: 0x4a00
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.
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”);
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
Example
void swap(int *x, int *y){ int temp;
temp = *x;*x = *y;*y = temp;
}
int num1 = 2, num2 = -3;swap(&num1, &num2);
(Program Continues)
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);
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
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]);
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);
Or Use Two functionsint Hours(int time){return (time / 60);
}
int Minutes(int time){return (time % 60);
}