1 / 33

Pointers

Pointers. Objectives. To describe what a pointer is To learn how to declare a pointer and assign a value to it To access elements via pointers To pass arguments by reference with pointers To understand the relationship between arrays and pointers

crevan
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. Objectives To describe what a pointer is To learn how to declare a pointer and assign a value to it To access elements via pointers To pass arguments by reference with pointers To understand the relationship between arrays and pointers To know how to access array elements using pointers To declare constant pointers and constant data

  3. Introduction to Pointers • Pointer variables, simply called pointers, are designed to hold memory addresses as their values • Normally, a variable contains a specific value, e.g., an integer, a floating-point value, and a character • However, a pointer contains the memory address of a variable that in turn contains a specific value

  4. Introducing Pointers • When we declare a variable, some memory is allocated for it. • Thus, we have two properties for any variable: • Its Address and • Its Data value • For Example, int count = 5;

  5. Introducing Pointers • How to get the memory-address of a variable? • Address of a variable can be accessed through the referencing operator “&” • Example: • &count will return memory location where the data value for “count” is stored. • A pointer variable is one that stores an address.

  6. Pointers Declaration • We can declare pointers as follows: • Type* varName; • Example: • int* pCount; • creates a pointer variable named “pCount”, that will store address (memory location) of some int type variable.

  7. Pointers Initialization • int var1 = 11; • int *ptr = &var1; • int var2 = 22; • int* ptr; • ptr = &var2; • int var2; • int* ptr = &var2; • cin>>*ptr;

  8. The Address Of Operator (&) • The & operator can be used to determine the address of a variable, which can be assigned to a pointer variable • Example: • int val = 100; • int* ptr; • ptr = &val; • Here, ptr points to the memory location, where val is stored. • val is an integer variable • ptr is an integer pointer

  9. The Dereferencing Operator * • C++ uses the * operator in yet another way with pointers • To access variable values pointed to by a pointer variable • Example: • int val = 100; • int* ptr = &val; • cout<<“ P points to the value: “<<*p; • Here, the * is the dereferencing operator and p is said to be dereferenced

  10. Example using Pointer, Address Of, and Derefrenecing Operator void main() { int count = 1000; int *pCount = &count; cout<<"The address of count is = "<<&count; cout<<"The address of count is = "<<pCount; cout<<"The value of count is = "<<count; cout<<"The value of count is = "<<*pCount; }

  11. Examples using Pointer, Address Of, and Derefrenecing Operator void main() { int var1 = 11; int var2 = 22; cout << &var1<<endl; cout<< &var2<<endl; int* ptr; ptr = &var1; cout << ptr << endl; ptr = &var2; cout << ptr << endl; } void main() { intval = 0; int* ptr = &val; *ptr = 500; cout << val << endl; cout << *ptr << endl; }

  12. Pointer Assignment • Assignment operator ( = ) is used to assign value of one pointer to another • Pointer stores addresses so p1=p2 copies an address value into another pointer • Example: • void main() • { • inta = 10; • int* pA = &a; • int* pB; • pB = pA; • cout<< *pA << endl; • cout<< *pB << endl; • }

  13. Pointer Assignment • Caution: • ptr1 = ptr2; • // changes the location that p1 "points" to • Is different from • *ptr1 = *ptr2; • // changes the value at location that p1 "points" to

  14. Pointer Assignment - Example void main() { int x = 40; int y = 20; int* ptr1, *ptr2; ptr1 = &x; ptr2 = &y; *ptr1 = *ptr2; cout << x << " " << y << endl; cout<<*ptr1<< " " <<*ptr2<<endl; }

  15. Pointer Assignment - Example void main() { int x = 40; int y = 20; int* ptr1, *ptr2; ptr1 = &x; ptr2 = &y; ptr1 = ptr2; cout << x << " " << y << endl; cout << *ptr1 << " " << *ptr2 << endl; }

  16. Pointer Assignment - Example void main() { intx = 40; inty = 20; int* ptr; ptr= &x; // ptr points to location of x *ptr = 50; // contents of x are updated ptr= &y; // ptr points to location of y *ptr = 30; // contents of y are updated cout<< x << " " << y << endl; }

  17. Pointer Type • A pointer variable is declared with a type such as int, double, etc. • You have to assign the address of the variable of the same type. • It is a syntax error if the type of the variable does not match the type of the pointer • Example: • int area = 1; • double *pArea = &area; // Wrong

  18. Pointer Type • All memory addresses are of the same length. Why is it important to declare the type of the variable that a pointer points to? • Reason: • To make sense for operation of the type “p++” where “p” is a pointer variable , the compiler needs to know the data type of the variable “p” points to. • Examples: • If “p” is a character-pointer then “p++” will increment “p” by one byte • if “p” is an integer-pointer its value on “p++” would be incremented by 4 bytes

  19. Pointer Type – void pointer void* is a pointer to no type at all. Any pointer type may be assigned to void * int iVar = 5; float fVar = 5.5; char cVar = ‘A’; int* ptr; ptr = &iVar; // Allowed ptr = &fvar; // Not Allowed ptr = &cVar; // Not Allowed int iVar = 5; float fVar = 5.5; char cVar = ‘A’; float* ptr; ptr = &iVar; // Not Allowed ptr = &fvar; // Allowed ptr = &cVar; // Not Allowed int iVar = 5; float fVar = 5.5; char cVar = ‘A’; char* ptr; ptr = &iVar; // Not Allowed ptr = &fvar; // Not Allowed ptr = &cVar; // Allowed int iVar = 5; float fVar = 5.5; char cVar = ‘A’; void* ptr; ptr = &iVar; // Allowed ptr = &fvar; // Allowed ptr = &cVar; // Allowed

  20. Arrays and Pointer • Arrays and pointers are closely related • Array name is the starting address of the array • int arr[3] = {10,20,30}; • cout<<arr; // Prints the address of the first element of array arr[0] • int ara[25]; int *ptr; • int i = 1, j = 3; • ptr = ara; • It means that ptr points to ara[0] • ptr + i points to ara[i] &ara[j] == ptr + j *(ptr+j) is the same as ara[j]

  21. Arrays and Pointer – Example void main() { char * str = “hello”; for(int j=0; j<5; j++) cout << *(str+j) << endl; }

  22. Arrays and Pointer – Example void main() { intintarray[5] = { 31, 54, 77,52,83}; for(intj=0; j<5; j++) cout<< intarray[j] << endl; } void main() { intintarray[5] = { 31, 54, 77,52,83}; for(int j=0; j<5; j++) cout << *(intarray + j) << endl; }

  23. Arrays and Pointer – Example int arr[] = { 31, 54, 77, 52, 93 }; int* ptr; ptr = arr; for(int j=0; j<5; j++) cout << *(ptr++) << endl;

  24. Function and Pointers • There are three ways to pass arguments to a function in C++ • Pass-by-value • Pass-by-reference • Pass-by-reference with pointer (Passing Address)

  25. Pass by Value – Example

  26. Pass by Reference – Example

  27. Pass by Reference with Pointers– Example

  28. Using const with Pointers A constant cannot be changed once it is declared. You can declare a constant pointer. For example, double radius = 5; const double * const pValue = &radius;

  29. Using const with pointers Nonconstant pointer to constant data const int* Ptr; Constant pointer to noncosntant data int* const Ptr; Constant pointer to constant data const int* const Ptr;

  30. Example double var1 = 10; double var2 = 20; const double * ptr = &var1; //*ptr = 100; Not Possible because value is constant ptr = &var2; // ok because value is not constant = = = = = = = = = = double var1 = 10; double var2 = 20; double * const ptr = &var1; *ptr = 100; // ok because value is not constant // ptr = &var2; Not Possible because ptr is constant = = = = = = = = = = = double var1 = 10; double var2 = 20; const double * const ptr = &var1; //*ptr = 100; Not Possible because value is constant //ptr = &var2; Not Possible because ptr is constant

  31. Pointers to Pointers • C++ allows the use of pointers that point to pointers, that these, in its turn, point to data (or even to other pointers). • char a; • char *b; • char **c; • a = 'z'; • b = &a; • c = &b; • cout<<**c;

  32. Pointer to string constant Syntax: char * string Literal void main() { char str1[] = “Defined as an array”; char* str2 = “Defined as a pointer”; cout<< str1 <<endl; cout<< str2 <<endl; //str1++; // can’t do this; str1 is a constant str2++; // this is ok, str2 is a pointer cout<< str2 <<endl; // efined as a pointer }

  33. Pointer to string constant // Copying string using Pointers void main() { char* str1 = “Self-conquest is the greatest victory.”; char str2[80]; //empty string char* src = str1; char* dest = str2; while( *src ) //until null character, *dest++ = *src++; //copy chars from src to dest *dest = ‘\0’; //terminate dest cout << str2 << endl; //display str2 }

More Related