Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.

Similar presentations


Presentation on theme: "Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable."— Presentation transcript:

1 Pointers

2 What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable l address can be stored in a variable of special type called pointer (variable) l C++ provides an abstraction of pointer n 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 name memory address i ’y’ c 2 0021 0022 0021 cp name memory address i ’y’ c 2 0021 0022 cp 2

3 Pointer Syntax l pointer variable is declared as follows: typeOfVariablePointedTo *pointerName; example: double *p; int *ip; l pointer declarations can be freely intermixed with ordinary variable declarations: char *cp, c1=’y’, c2=’n’; int i, *ip; l star can move to type without changing semantics: int *i, j; is the same as int* i, j; pointer to a pointer is legal and sometimes used: char **cpp; pointer can be assigned value using & (address of or reference) operator: cp = &c1; // until reassigned cp ”points at” c1 the value of a variable the pointer points to can be accessed using * (dereference) operator: cout << *cp << endl; *cp = ’G’; l note that star at declaration is not a dereference operator – it just signifies that the variable is a pointer. 3

4 Pointer Usage pointer can be assigned a value at declaration char *cp2=&c2; int *ip; l pointer variable can point to multiple variables (in sequence) and multiple pointers can point at the same variable l what does this code fragment do? int *ip1, *ip2, one=1, two=2; ip1=&one; ip2=ip1; *ip1 = *ip1 + 1; ip1=&two; *ip1 -= 1; cout << *ip2 << ” ” << *ip1; 4

5 Constants and Pointers l a constant pointer is a pointer construct where we cannot change the location to which the pointer points char c = 'c'; const char d = 'd'; char *const ptr1 = &c; ptr1 = &d; // illegal l a pointer to a constant value is a pointer object where the value at the location to which the pointer points is considered constant const char *ptr2 = &d; *ptr2 = 'e'; // illegal: cannot change d // through dereferencing ptr2 l the following also declares a pointer to a constant char const *ptr2 = &d; 5

6 Array Names and Constant Pointers l array name is in fact a constant pointer l 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 l an array name can be used as name and as pointer: a[3]=22; // as array name: applying indexing p = a;// as pointer l a pointer can also be used similarly p[4]=44; // as name p = a;// as pointer l since array name is a constant pointer – its modification is not legal a=p; // ERROR! 6

7 Pointer Arithmetic l array elements are guaranteed to be in continuous memory locations l adding one to pointer advances it one memory location of specified type int a[5], *p = a; p = p + 1; // p points to second element of array l gives alternative way to manipulate arrays l allowed pointer operations: add/subtract integer, compound assignment, increment, decrement, subtract two pointers of the same type (what’s the purpose of that?) p++; // moves p one position to the right – points to // third element of array p -=2; // moves p two positions to the left cout << p – a; // prints how many elements between p and a l other arithmetic operations, like pointer division or multiplication, are not allowed l regular and pointer arithmetic operations can be intermixed *(p++) = 22; // what does this do? l caution n use only on continuous memory locations n terse but obscure –indexing may be clearer to understand –error prone 7

8 Null Pointer/Loose Pointer Problem l a pointer that is not initialized holds arbitrary value l assigning a value to the location uninitialized pointer points to can lead to unpredictable results: loose (dangling) pointer problem int *ptr; *ptr = 5; // ERROR - loose pointer! l what do you think the result of this assignment is? nullptr is a constant that is assigned to a pointer that does not have a value int *ptr = nullptr; assigning nullptr to pointer does not eliminate loose pointer problem but it is a convenient constant to compare to int *ptr2 = nullptr, i=5; *ptr2 = 5; // ERROR - still loose if (ptr2 == nullptr) ptr2=&i; cout << *ptr2; 8

9 Pointers to Objects l pointers can point to objects: myclass{ public: void setd(int i){d=i;}; int getd() const {return d;}; private: int d; }; myclass ob1, *obp=&ob1; members can be accessed using pointers: (*obp).setd(5); parentheses around (*obp) are needed because dot-operator has higher precedence than dereferencing a shorthand -> is used for accessing members of the object the pointer points to: cout getd(); 9


Download ppt "Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable."

Similar presentations


Ads by Google