1 / 9

Pointers

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

naasir
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers

  2. What Is A Pointer c ’y’ 0021 i 2 0022 cp 0021 name memory address c ’y’ 0021 i 2 0022 cp name memory address • every variable has a memory address char c=’y’; int i=2; • address of variable i is 0022 • address can used to refer to this variable • address can be stored in a variable of special type called a pointer (variable) • C++ provides an abstraction of a pointer • a pointer is used only to reference the variable it points to - we usually don’t think of pointers as holding an integer (address) but just a reference to a variable

  3. Pointer Syntax • A pointer variable is declared as follows: typeOfVariablePointedTo *pointerName; • example: double *p; int *ip; • pointer declarations can be freely intermixed with ordinary variable declarations: char *cp, c1=’y’, c2=’n’; int i, *ip; • The star can move to the type without changing semantics: int *i, j; is the same as int* i, j; • A pointer to a pointer is legal and sometimes used: char **cpp; • A pointer can be assigned a 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’;

  4. Using Pointers • note that the star in the declaration is not a dereference opeartor – it just signifies that the variable is a pointer. • A pointer can be initialized in the declaration just like any other variable: char *cp2=&c2; int *ip=&i; • Apointer variable can point to multiple variables (in sequence) and multiple pointers can point at the same variable • 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;

  5. Constants and Pointers • 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 • 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 • the following also declares a pointer to a constant char const *ptr2 = &d;

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

  7. Pointer Arithmetic • array elements are guaranteed to be in continuous memory locations • adding one to a pointer advances it one memory location of the specified type int a[5], *p = a; p = p + 1; // p points to the second element of array • gives alternative way to manipulate arrays • 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 • other arithmetic operations, like pointer division or multiplication, are not allowed • regular and pointer arithmetic operations can be intermixed *(p++) = 22; // what does this do? • caution • use only on continuous memory locations • terse but obscure • indexing is clearer to understand • error prone

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

  9. 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 ob1, *obp=&ob1; • members can be accessed using pointers: (*obp).setd(5); • parentheses around (*obp) are needed because dot-operator has higher priority than dereferencing • a shorthand -> is used for accessing members of the object the pointer points to: cout << obp->getd();

More Related