Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Similar presentations


Presentation on theme: "Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the."— Presentation transcript:

1 Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the address of an int; memory is allocated for the address (pointer) but not the int

2 Pointing to an existing int int i = 25; Allocates memory for an int and stores the valkue 25 int * iptr; Allocates memory for a pointer to an int iptr = & i; sets the value of iptr to the address of i i and *iptr are the same –changing either one will change the other

3 Creating a new int int j = 15; Allocates momory for an int and stores the value 15 int * jptr; = new int; sets aside memory for an int and puts the address int j *j = j; stores value of j in memory pointed to by jptr

4 Pointers and Dynamic arrays A dynamic array is declared as a pointer int * iArray; use new to allocate appropriate amount of memory iArray = new int[ desiredSize]; use iArray just like you use any array iArray[index] = someValue;

5 Changing the size Allocate a new block of memory with new int *temp = new int[newSize]; Copy the elements in the current memory for (int j=0; j<currentSize; j++) temp[j] = iArray[i]; delete the old memory –delete [] iArray; reassign the pointer iarray = temp;

6 Pointers and the “new” Operator Pointer Declarations –pointer variable of type “pointer to double” –can store the address of a double t in p double *p; The new operator creates a variable of type double & puts the address of the variable in pointer p p = new double; Dynamic allocation - memory is allocated while the program is running instead of before it starts

7 Pointers Actual address has no meaning Form:type*variable; Example: double *p; ? P

8 new Operator Actually allocates storage Form:new type; // one memory location new type [n]; // n memory locations Example:p = new double;

9 Pointer Variables If they aren't used for arrays, you have to use them differently

10 Accessing Data with Pointers * - indirection operator *p = 15.5; Stores floating value 15.5 in memory location *p - the location pointed to by p 15.5 p

11 Pointer Statements double*p; p = new doublet; *p = 15.5; cout << “The contents of the memory cell pointed to by p is “ << *p << endl; Output The contents of memory cell pointed to by p is 15.5

12 Pointer Operations Pointers can only contain addresses So the following are errors: p = 1000; p = 15.5; You need to assign an address to p p = &varOfAppropriateType

13 Pointer Operations Assignment of pointers if q & p are the same pointer type q = p; p and q both refer to the same memeory location - the same variable relational operations == and != compare addresses not the values stored at those addresses

14 Pointers to Objects class electric { public: string current; int volts; }; electric*p,*q; p and q are pointers to objects of type electric

15 Pointers to objects p = new electric; Allocates storage for struct of type electric and places address into pointer p ?? currentvoltsp

16 Assignments *p.current = “AC”; *p.volts = 115; Statements above can also be written –p ->current = “AC”; –p ->volts = 115; AC115 currentvoltsp

17 Member Access via Pointers From:p ->m Example:p ->volts Example: –cout current volts << endl; Output –AC115

18 Pointers to Objects q = new electric; Allocates storage for object of type electric and places address into pointer q Copy contents of p struct to q struct *q = *p; AC115 q->currentq->voltsq

19 Pointers to Objects q->volts = 220; q = p; AC220 q->currentq->voltsq AC 115 AC220 q pq->currentq->volts p->currentp->volts

20 4/24/02 Section 2 Final: Wed May 15, 1:00-3:00 pm –one problem from each of last two exams –practice exams Program 6 - due May 3 –demos - May 1 - May 8 –anyone who finishes early can make arrangements to come in sooner

21 Appointment Class Want to write a program to make appointments for demos for program 6 What should go into Appointment class? –need extra classes Time Date How do we use the class?

22 Appointment.h class Appointment { public: private: } Appointment(); // constructors // set and get functions Time start, end; Date day; String names[][], notes, userID; int howMany;

23 13.2 Manipulating the Heap When new executes where is struct stored ? Heap –C++ storage pool available to new operator Effect of p = new node; Figure 14.1 shows Heap before and after executing new operator

24 Effect on new on the Heap

25 Returning Cells to the Heap Operation –delete p; Returns cells back to heap for re-use When finished with a pointer delete it Watch dual assignments and initialization Form:deletevariable; Example:deletep;

26 13.3 Linked Lists Arrange dynamically allocated structures into a new structure called a linked list Think of a set of children’s pop beads Connecting beads to make a chain You can move things around and re-connect the chain We use pointers to create the same effect

27 Children’s Beads

28 Declaring Nodes If a pointer is included in a struct we can connect nodes struct node { string word; int count; node *link; }; node*p, *q, *r;

29 Declaring Nodes Each var p, q and r can point to a struct of type node –word(string) –count(int) –link(pointer to a node address) wordcountlink Struct of type node String Integer Address

30 Connecting Nodes Allocate storage of 2 nodes p = new node; q = new node; Assignment Statements p->word = “hat”; p->count = 2; q->word = “top”; q->count = 3;

31 Figure 13.3

32 Connecting Nodes Link fields undefined until assignment p->link = q; Address of q is stored in link field pointed to by p Access elements as follows q->word or p->link->word Null stored at last link field q->link = NULL; or p->link->link = NULL;

33 Connecting Nodes

34 Inserting a Node Create and initialize node r = new node; r->word = “the”; r->count = 5; Connect node pointed to by p to node pointed to by r p->link = r; Connect node pointed to by r to node pointed to by q r->link = q;

35 Inserting a New Node in a List

36 Insertion at Head of List OldHead points to original list head oldHead = p; Point p to a new node p = new node; Connect new node to old list head p->link = oldHead;

37 Insertion at Head of List

38 Insertion at End of List Typically less efficient (no pointer) Attach new node to end of list last->link = new node; Mark end with a NULL last->link->link = NULL;

39 Insertion at End of List

40 Deleting a Node Adjust the link field to remove a node Disconnect the node pointed to by r p->link = r->link; Disconnect the node pointed to by r from its successor r->link = NULL; Return node to Heap delete r;

41 Deleting a Node

42 Traversing a List Often need to traverse a list Start at head and move down a trail of pointers Typically displaying the various nodes contents as the traversing continues Advance node pointer head = head->link; Watch use of reference parameters

43 PrintList.cpp // FILE: PrintList.cpp // DISPLAY THE LIST POINTED TO BY HEAD void printList (listNode *head) { while (head != NULL) { cout word count << endl; head = head->link; }


Download ppt "Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the."

Similar presentations


Ads by Google