240 likes | 365 Views
Pointers. Introduction. Main memory is a sequence of memory locations that are addressed 0, 1, 2, … Pointers are variables that contain memory addresses as their values. Normally, a variable directly contains a specific value. Hence, a variable name directly references a value.
E N D
Introduction • Main memory is a sequence of memory locations that are addressed 0, 1, 2, … • Pointers are variables that contain memory addresses as their values. • Normally, a variable directly contains a specific value. Hence, a variable name directly references a value. • A pointer contains an address of a variable that contains a specific value. Hence, a pointer indirectly references a value. • A variable containing an address of some location/variable is said to be a pointer to that location/variable.
Pointers, like all variables, must be defined before they are used in a program. • The definition: int *countPtr, count; specifies that variable countPtr is of type int *. • This means, that variable countPtr is a pointer to an integer. • The definition is read as: countPtr is a pointer to int or countPtr points to an object of type int • The * in the definition indicates that the variable being defined is a pointer. • The * does not apply to the count variable which is a normal variable.
Pointers can be defined to point to objects of any data type (integers, doubles, etc.) • Pointers can be created for different objects (simple variables, structures, etc.) • It is recommended (not necessary) to include the letter ptr in pointer variable names to distinguish them from other kinds of variables. • Pointers should be initialized either when they are defined or in an assignment statement.
A pointer may have a zero, a NULL, or an address value. • Initializing the pointer variable to NULL (or 0) value means that the pointer does not point to any memory location (points to nothing). • Zero is the only integer value that can be assigned to a pointer. • Size of a pointer is often 4 bytes
Pointer to void • pointer to void (type void *) • Generic pointer, represents any type • No casting needed to convert a pointer to void pointer Example: int *ptr1; void *ptr2; ptr2 = ptr1; ptr1 = (int *)ptr2;
Pointer Operators • & (address operator) • a unary operator (applies to one value or variable). • It returns the address of its operand. • The & address operator cannot be applied to constants or expressions. int y = 5; int *yPtr; yPtr = &y; /* assigns the address of the variable y to pointer variable yPtr. We say, yPtr points to y */
yptr y y 5 yPtr Address of y is value of yptr 500000 600000 600000 5 Graphical representation of a pointer pointing to an integer variable in memory. int y = 5; int *yPtr; yPtr = &y;
* (indirection/dereferencing operator) • returns the value of the object to which its operand points • *yptr returns y (because yptr points to y) cout<<*yPtr; /* this statement prints the value of the variable y, which is 5 */ • * can be used for assignment *yptr = 7; /* changes y to 7 */ • * and & are inverses • They cancel each other out
#include <iostream.h> void main() { int a; // a is an integer int *aPtr; // aPtr is a pointer to an integer a = 7; aPtr = &a; // aPtr assigned address of a cout << "The address of a is " << &a << "\nThe value of aPtr is " << aPtr; cout << "\n\nThe value of a is " << a << "\nThe value of *aPtr is " << *aPtr; cout << "\n\nShowing that * and & are inverses of " << "each other.\n&*aPtr = " << &*aPtr << "\n*&aPtr = " << *&aPtr << endl; * and & are inverses of each other
&a aPtr The address of a is 0012FED4 The value of aPtr is 0012FED4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012FED4 *&aPtr = 0012FED4 int a; int *aPtr; a = 7; aPtr = &a;
The following 3 statements are all equal: cout<<yPtr; cout<<&*yPtr; cout<<*&yPtr; • The printf conversion specifier %p outputs the memory location as a hexadecimal integer. printf("%p",yPtr); printf("%p",&*yPtr); printf("%p",*&yPtr);
Examples int *w; //w is a pointer variable to an integer int t=5; //t is an integer variable that equals 5 w=&t; //w stores the address of t i.e. points to t cout<<t; //5 is displayed cout<<*w; //5 is displayed t++; //t = 6 cout<<*w; //6 is displayed *w=*w+2; //*w is 8 cout<<*w; //8 is displayed cout<<t; //8 is displayed
float r; //r is a float variable w=&r; //syntax error because w is //declared as a pointer to an integer int N; w=&N; //w points to N w=NULL; //w points to nothing in memory cout<<*w; //run-time error t=8; w=&t; //w points to t cout<<&t; // 0012FF78 cout<<w; //0012FF78 The address displayed may be any address in memory. But it will be the same for w or &t.
cout<<t; //8 is displayed cout<<*t //error, the indirection //operator is only used for pointers cout<<&*w; // 0012FF78 cout<<*&w; // 0012FF78
Pointer Operations 1. A limited set of arithmetic operations may be performed on pointers: 2. Pointers can be compared using equality and relational operators. 3. A pointer can be assigned to another pointer if both pointers are of the same type. However, a cast operator can be used to allow this operation. • The generic pointer (void *) can be assigned to any pointer and any pointer can be assigned to it. However, a generic pointer cannot be dereferenced. • To dereference a void pointer it must be type casted to some pointer type.
Pointer operations (Cont) • A pointer may be incremented (++) or decremented (--). • An integer may be added to a pointer (+ or +=). • New Address = Old Address + (integer * size of object data type) • An integer may be subtracted from a pointer (- or -=). • New Address = Old Address - (integer * size of object data type) • One pointer may be subtracted from another. • Subtraction Result = Difference/size of object to which the pointer refers
Example int x; int *y=&x; y++; //increases y by 2 y+=5; //increases y by 10 double z; double *w=&z; w--; //decreases w by 8 w-=5; //decreases w by 40
Pointers Comparison • Pointers of the same data type can be compared using equality and relational operators. • The most common operation is to use == and != to determine if 2 pointer variables point to the same memory location or not.
Example (1) int *iPtr, *jPtr; if(iPtr == jPtr) //returns true iff the address in iPtr is the same as the address in jPtr. int *nPtr; double *dPtr; if(nPtr = = dPtr) //error: not valid statement because the 2 pointers are of different data types The null address can be compared with any pointer variable. if(nPtr==0) if(dPtr==0) //Both of the above statements are valid/legal statements //The expressions evaluate to true if both pointers were pointing to nothing in memory.
Pointers Assignment • Pointer variables can be assigned the values of other pointer variables that are of the same data type • If jPtr and iPtr are 2 pointers of the same data type, then the result of the expression jPtr = iPtr; is that the value of iPtr will be copied to jPtr so that they both have the same memory location. • the result of the expression *jPtr = *iPtr; is that the value pointed to by jPtr will be replaced by the value pointed to by iPtr so that the variables to which the pointers are pointing will both have the same value.
11 i j 22 11 *iPtr *jPtr = *iPtr; . . . 0xbffffd20 iPtr *jPtr 0xbffffd24 jPtr Before the Assignment 11 i j 22 11 *iPtr . . . iPtr 0xbffffd20 *jPtr 0xbffffd20 jPtr After the Assignment jPtr=iPtr
Pointers of the same type can be assigned to each other • If not the same type, a cast operator must be used • Exception: pointer to void (type void *) • Generic pointer, represents any type • No casting needed to convert a pointer to void pointer Example: int *ptr1; void *ptr2; ptr2 = ptr1; ptr1 = (int *)ptr2;
Operators Parenthesis ( ) [ ] Unary + - ++ -- ! * & (type) / % + - < <= > >= == != && || = += -= *= /= %= Associativity Left to Right Right to Left Left to Right Left to Right Left to Right Left to Right Left to Right Left to Right Right to Left Operator Precedence