Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007-2014 – Curt Hill Pointers A Light Introduction.

Similar presentations


Presentation on theme: "Copyright © 2007-2014 – Curt Hill Pointers A Light Introduction."— Presentation transcript:

1 Copyright © 2007-2014 – Curt Hill Pointers A Light Introduction

2 Copyright © 2007-2014 – Curt Hill What are pointers? Memory addresses –Restricted to point at a certain type of thing A pointer may refer to any address within memory –With some restrictions What do we use pointers for? –C used them for reference parameters –Dynamic arrays –Dynamic data structures

3 Copyright © 2007-2014 – Curt Hill Declaration Declaration: –int * iptr; –money * mptr; Declaration involves * attached to a name –int *ip, k, *jp; –ip and jp are pointers –k is an integer

4 Copyright © 2007-2014 – Curt Hill What kind of values? C/C++ thinks of a pointer as a special type of integer Thus int * ip = 5; is legal but foolish –It refers to a location in the operating system –Using it will usually kill the program

5 Copyright © 2007-2014 – Curt Hill Values There are three normal ways to put a value into a pointer Assignment from similar pointer –Copies the value Using the address of operator –This will be discussed next Using new (in C++) or malloc (in C) –This will be discussed in a subsequent presentation

6 Copyright © 2007-2014 – Curt Hill Address of Operator The unary & is the address of operator It takes whatever follows it and obtains the address –This is usually assigned to a pointer Thus we could do this: int i = 5; int * ip = &i; The address of i is now contained in ip

7 Copyright © 2007-2014 – Curt Hill Using a pointer Two ways to refer to a pointer –With or without a prefix asterisk –Without refers to the pointer –With refers to what it points at Example –int * ip; –ip is the pointer –*ip is a pointer dereference –*ip refers to thing pointer points at

8 Copyright © 2007-2014 – Curt Hill Visualization Pointers have a memory address as a value, rather than a conventional value We do not care about the integer value of the address We do care about what is occupying the location referred to by the address Therefore we usually use pictures to represent

9 Copyright © 2007-2014 – Curt Hill Some sample code Consider the following code: int * ip, val = 2; ip = &val; *ip = 5; cout << val;

10 Copyright © 2007-2014 – Curt Hill Initially int * ip, val = 2; ip = &val; *ip = 5; cout << val; ip val 2

11 Copyright © 2007-2014 – Curt Hill Load the Address int * ip, val = 2; ip = &val; *ip = 5; cout << val; ip 2 val

12 Copyright © 2007-2014 – Curt Hill Use the Address int * ip, val = 2; ip = &val; *ip = 5; cout << val; ip 5 val

13 Copyright © 2007-2014 – Curt Hill Output the Value int * ip, val = 2; ip = &val; *ip = 5; cout << val; ip 5 val Output the 5

14 Copyright © 2007-2014 – Curt Hill Discussion ip contains the address of val Thus *ip and val are synonyms Both refer to the same value and either one may change it or use it

15 Copyright © 2007-2014 – Curt Hill C and pointers In C there is only one parameter passage mechanism, call by value –A copy of the item is passed –However, value parameters cannot disarm a pointer We sometimes called it passing it by reference until we get to C++ and have another kind of passing by reference –C++ does it the way it should have been done the first time, since the syntax is cleaner

16 Copyright © 2007-2014 – Curt Hill C++ Swap Example In C++ we would do this: void swap(int & a, int& b){ int temp = a; a = b; b = temp; } // called with swap(x,y);

17 Copyright © 2007-2014 – Curt Hill C Swap In C we would do this: void swap(int * a, int * b){ int temp = *a; *a = *b; *b = temp; } // called with swap(&x,&y);

18 Copyright © 2007-2014 – Curt Hill Discussion The & as the mark of a reference parameter in C++ is rather natural for those who know C Instead of the programming using the address of operator in the call the compiler does that for us –As well as the pointer dereference within the function

19 Copyright © 2007-2014 – Curt Hill C++ Rules The C++ version is better because it is both easier and less error prone It was very easy to leave out an asterisk in swap Since pointers will be cast to and from integers there would be no syntax error However a very interesting pointer dereference could occur

20 Copyright © 2007-2014 – Curt Hill Conclusion It may not be clear yet but: Pointers are a very powerful and very dangerous item Pointers and arrays are inseparable in C/C++ The smart programmer will confine all pointers to the private or protected data of a class –The client may use the power without risking the danger We will return for more to the topic of pointers


Download ppt "Copyright © 2007-2014 – Curt Hill Pointers A Light Introduction."

Similar presentations


Ads by Google