Pointers What is Pointer Every variable has memory
Pointers
What is Pointer • Every variable has memory address char c=‘y’; int i=2; name memory address c i ‘y’ 2 0021 0022 • Address of variable i is 0022 • Address can be used to refer to this variable • Address can be stored in a variable of special type called pointer (variable) • C++ provides an abstraction of pointer – pointer is used only to refer to the variable it points to - we usually don’t think of pointers as holding integer (address) just a reference to a variable cp 0021 name memory address c i ‘y’ 2 cp 0021 0022
Pointer Syntax • Pointer variable is declared as follows: type_of_variable_pointed_to *pointer_name; double *p; int *ip; • Pointer declarations can be freely intermixed with ordinary variable declarations: char *cp, c 1=‘y’, c 2=‘n’; int i, *ip, ; • Pointer to a pointer is legal and sometimes used: char **cpp; • Pointer can be assigned value using & (address of) operator: cp = &c 1; // until reassigned cp “points at” c • Value of a variable the pointer points to can be accessed using * (dereferencing) operator: cout << *cp << endl; *cp = ‘G’;
Using Pointers • Note: star at declaration is not a dereference operator • it just signifies that the variable is a pointer • Pointer can be assigned a value at declaration char *cp 2=&c 2; int *ip; • Pointer variable can point to multiple variables (in sequence) and multiple pointers can point at the same variable • what does this piece of code do? int *ip 1, *ip 2, one=1, two=2; ip 1=&one; ip 2=ip 1; *ip 1 = *ip 1 + 1; ip 1=&two; *ip 1 -= 1; cout << *ip 2 << “ “ << *ip 1;
Constants and Pointers • Constant pointer is a pointer object where we cannot change the location to which the pointer points char c = 'c'; const char d = 'd'; char *const ptr 1 = &c; ptr 1 = &d; // illegal • Pointer to a constant value is a pointer object, where the value at the location the pointer points to, is considered constant const char *ptr 2 = &d; *ptr 2 = 'e'; // illegal: cannot change d // through dereferencing ptr 2 • Following also declares a pointer to a constant char const *ptr 2 = &d;
Array Names and Constant Pointers • Array name is in fact a constant pointer • Example int *p; // this is a pointer int a[SIZE]; // this is an array // int *const a; plus memory allocation // is equivalent p = a; // now pointer references first // element of an array • Array name is a constant pointer => its modification is not legal a = p; // ERROR!
Array Names and Constant Pointers • Array name can be used as a name and a pointer a[0]= 22; //as a name *a = 11; //as a pointer cout << a[0]; //prints 11 • Pointer can also be used as a name p[4]= 44; //as name *p = 10; //as a pointer cout << a[0] << endl; // prints 10 cout << a[4]; //prints 44
Null Pointer/ Lose Pointer Problem • Pointer that is not initialized holds an arbitrary value • Assigning a value to the location, uninitialized pointer points to, can lead to unpredictable results: loose pointer problem int *ptr; *ptr = 5; // ERROR - lose pointer! // segmentation fault • NULL is a constant that is assigned to a pointer that does not have a value int *ptr = NULL; • Assigning NULL to pointer does not eliminate lose pointer problem but it is convenient constant to compare to int *ptr 2 = NULL, i=5; *ptr 2 = 5; // ERROR - still loose if (ptr 2 == NULL) ptr 2=&i; cout << *ptr 2;
Pointers to Objects • Pointers can point to objects: myclass{ public: void setd(int i){d = i; }; int getd() const {return d; }; private: int d; }; myclass ob 1, *obp = &ob 1; • Members can be accessed using pointers: (*obp). setd(5); • Parentheses around (*obp) are needed because dot-operator has higher priority than dereferencing • Shorthand -> is used for accessing members of the object the pointer points to: cout << obp->getd();
- Slides: 9