Pointers
description
Transcript of Pointers
![Page 1: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/1.jpg)
Pointers
![Page 2: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/2.jpg)
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
![Page 3: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/3.jpg)
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
![Page 4: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/4.jpg)
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;
1245064
5
count
![Page 5: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/5.jpg)
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.
1245064
5
&count
count
![Page 6: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/6.jpg)
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.
Location of pCount
1245064 1245064
5
&count
count pCount
![Page 7: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/7.jpg)
Pointers Initialization
int var1 = 11;int *ptr = &var1;
int var2 = 22;int* ptr; ptr = &var2;
int var2;int* ptr = &var2;cin>>*ptr;
![Page 8: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/8.jpg)
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
![Page 9: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/9.jpg)
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
![Page 10: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/10.jpg)
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;
}
![Page 11: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/11.jpg)
Examples using Pointer, Address Of, and Derefrenecing Operator
void main(){
int val = 0;int* ptr = &val;*ptr = 500;cout << val << endl;cout << *ptr << endl;
}
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;
}
![Page 12: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/12.jpg)
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(){
int a = 10;int* pA = &a;int* pB;pB = pA;cout << *pA << endl;cout << *pB << endl;
}
![Page 13: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/13.jpg)
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
![Page 14: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/14.jpg)
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;
}
![Page 15: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/15.jpg)
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;}
![Page 16: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/16.jpg)
Pointer Assignment - Example
void main(){
int x = 40;int y = 20;int* ptr;ptr = &x; // ptr points to location of x*ptr = 50; // contents of x are updatedptr = &y; // ptr points to location of y*ptr = 30; // contents of y are updatedcout << x << " " << y << endl;
}
![Page 17: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/17.jpg)
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
![Page 18: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/18.jpg)
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
![Page 19: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/19.jpg)
Pointer Type – void pointervoid* 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
![Page 20: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/20.jpg)
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]
![Page 21: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/21.jpg)
Arrays and Pointer – Example
void main(){ char * str = “hello”; for(int j=0; j<5; j++) cout << *(str+j) << endl;}
![Page 22: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/22.jpg)
Arrays and Pointer – Example
void main(){ int intarray[5] = { 31, 54, 77,52,83}; for(int j=0; j<5; j++) cout << intarray[j] << endl;}
void main(){ int intarray[5] = { 31, 54, 77,52,83}; for(int j=0; j<5; j++) cout << *(intarray + j) << endl;}
![Page 23: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/23.jpg)
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;
![Page 24: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/24.jpg)
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)
![Page 25: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/25.jpg)
Pass by Value – Example
void func(int num){ cout<<"num = "<<num<<endl; num = 10; cout<<"num = "<<num<<endl;}
void main(){ int n = 5; cout<<"Before function call: n = "<<n<<endl; func(n); cout<<"After function call: n = "<<n<<endl;}
![Page 26: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/26.jpg)
Pass by Reference – Example
void func(int &num){ cout<<"num = "<<num<<endl; num = 10; cout<<"num = "<<num<<endl;}
void main(){ int n = 5; cout<<"Before function call: n = "<<n<<endl; func(n); cout<<"After function call: n = "<<n<<endl;}
![Page 27: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/27.jpg)
Pass by Reference with Pointers– Example
void func(int *num){ cout<<"num = "<<*num<<endl; *num = 10; cout<<"num = "<<*num<<endl;}
void main(){ int n = 5; cout<<"Before function call: n = "<<n<<endl; func(&n); cout<<"After function call: n = "<<n<<endl;}
![Page 28: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/28.jpg)
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;
const double * const pValue = &radius;
Constant data Constant pointer
![Page 29: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/29.jpg)
Using const with pointers
Nonconstant pointer to constant dataconst int* Ptr;
Constant pointer to noncosntant dataint* const Ptr;
Constant pointer to constant dataconst int* const Ptr;
![Page 30: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/30.jpg)
Exampledouble var1 = 10;double var2 = 20;const double * ptr = &var1;//*ptr = 100; Not Possible because value is constantptr = &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
![Page 31: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/31.jpg)
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;
![Page 32: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/32.jpg)
Pointer to string constant
Syntax: char * string Literalvoid 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 constantstr2++; // this is ok, str2 is a pointer
cout<< str2 <<endl; // efined as a pointer }
![Page 33: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022052414/56815438550346895dc23a1a/html5/thumbnails/33.jpg)
Pointer to string constant// Copying string using Pointersvoid main(){char* str1 = “Self-conquest is the greatest victory.”;char str2[80]; //empty stringchar* 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}