Your SlideShare is downloading. ×
Lec 37 -  pointers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lec 37 - pointers

89
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
89
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Student Book
  • Student Book
  • Student Book
  • Transcript

    • 1. Chapter: 10 Pointers Lecture: 37 Date: 16.10.2012
    • 2. What are Pointers Used For? Accessing array elements Passing arguments to a function when the function needs to modify the original argument Passing arrays and strings to functions Obtaining memory from the system Creating data structures such as linked lists
    • 3. Memory and Addresses 1270 1271 1272 1273 1274 1275 Computer Memory
    • 4. Memory and Addresses Addresses Locations 1270 1271 1272 1273 1274 1275 Computer Memory
    • 5. Memory and Addressesint IntVar1; //2 bytesint IntVar2; //2 byte
    • 6. Memory and Addresses Addresses Locations 1270 1271int IntVar1; //2 bytes 1272 IntVar1int IntVar2; //2 byte 1273 1274 1275 IntVar2 Computer Memory
    • 7. Memory and Addressesint IntVar1 = 25;int IntVar2 = 11;
    • 8. Memory and Addresses Addresses Locations 1270 1271int IntVar1 = 25; 25 IntVar1 1272int IntVar2 = 11; 1273 1274 11 IntVar2 1275 Contents/Data
    • 9. Memory and Addresses In some cases we may be interested in knowing the address where our variable is being stored during runtime. The address that locates a variable within memory is what we call a reference to that variable. e.g., & IntVar; Address-of/reference When preceding the name of the variable “IntVar” with the operator reference operator (&) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory).
    • 10. Memory and Addresses#include <iostream>#include <conio.h>using namespace std;int main(){int IntVar1;int IntVar2;cout << &IntVar1 << endl //print the addresses << &IntVar2 << endl;getch();return 0; }
    • 11. Pointer Variable The variable that stores the reference to another variable is what we call a pointer. e.g., ptr = &InVar;
    • 12. Pointer Variable The variable that stores the reference to another variable is what we call a pointer. e.g., Pointer-to Pointer/Pointer-variable int * ptr; //variable “ptr” as a pointer-to “int” ptr = &InVar;
    • 13. Accessing Addressesint main(){ int IntVar1 = 25; int IntVar2 = 11; int* ptr; //pointer to integers ptr = &IntVar1; //pointer points to IntVar1 cout << ptr << endl //print the address of IntVar1 ptr = &IntVar2 cout << ptr << endl //print the address of IntVar2 getch(); return 0; }
    • 14. int* ptr; 1270ptr = &IntVar1; ptr 1271 cout << ptr ; 1271 25 IntVar1 1272 ptr points-to to the 1273 address of IntVar1 1274 11 1275 IntVar2 1270 1271 25 1272 IntVar1int* ptr; ptr 1273ptr = &IntVar2; 1274 1274 cout << ptr ; 11 ptr points-to to the 1275 IntVar2 address of IntVar2
    • 15. Accessing Contensint main(){ int IntVar1 = 25; int IntVar2 = 11; int* ptr; //pointer to integers ptr = &IntVar1; //pointer points to IntVar1 cout << *ptr << endl //print the content of IntVar1 ptr = &IntVar2 cout << *ptr << endl //print the content of IntVar2 getch(); return 0; }
    • 16. ptrint* ptr;ptr = &IntVar1; 25 IntVar1 *ptr is 25 cout << *ptr ; 11 deference /indirection operator. IntVar2 Expression *ptr means the value of the variable pointed to by ptr. 25 IntVar1 ptrint* ptr;ptr = &IntVar2; 11 *ptr is 11 IntVar2 cout << *ptr ;
    • 17. Pointer to Void The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt = &floVar;
    • 18. Pointer to Void The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt = &floVar; //ERROR: can’t assign float* to int*
    • 19. Pointer to Void The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt; ptrInt = &floVar; //ERROR: can’t assign float* to int* Exception to that case is a general-purpose pointer that can point to any data type, e.g., void* ptrVoid; //pointer to void
    • 20. Pointer to Void The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt; ptrInt = &floVar; //ERROR: can’t assign float* to int* Exception to that case is a general-purpose pointer that can point to any data type, e.g., void* ptrVoid; //pointer to void ptrVoid = &floVar; //OK
    • 21. Counting by Integers - Arrays
    • 22. Passing Arguments to Functions Arguments can be passed to functions in three different ways: (i) by value, (ii) by reference, and (iii) by pointers A function can change the values in a calling function if the arguments are passed by a reference or by a pointer.
    • 23. Pass-by-Referencevoid centimize(double& );int main(){ double var = 2.5; centimize(var); cout << var << endl;getch(); return 0; }void centimize(double& v){ v = v * 100; }
    • 24. Pass-by-Pointervoid centimize(double* );int main(){ double var = 2.5; centimize(&var); cout << var << endl;getch(); return 0; }void centimize(double* ptrd){ *ptrd = *ptrd * 100; }
    • 25. Pointer Passed to Function
    • 26. Passing Arrays to Functionconst int MAX = 5;void centimize(double*); //prototypeint main(){ double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 }; centimize(varray); for(int j=0; j<MAX; j++) cout << varray[j] << endl;getch(); return 0; }void centimize(double* ptrd){ for(int j=0; j<MAX; j++) *ptrd++ = *ptrd * 2.54; } //*ptrd++ = *(ptrd++)
    • 27. Pointer Passed to Function
    • 28. Linked List Linked list is another way to store data besides storing data in arrays. However, arrays suffer from the necessity to declare a fixed-size array before running the program.

    ×