Pointers
description
Transcript of 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 To know how to access array elements using pointers To declare constant pointers and constant data
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
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
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
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
Pointers Initialization
int var1 = 11;int *ptr = &var1;
int var2 = 22;int* ptr; ptr = &var2;
int var2;int* ptr = &var2;cin>>*ptr;
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
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
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;
}
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;
}
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;
}
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
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;
}
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;}
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;
}
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
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
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
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]
Arrays and Pointer – Example
void main(){ char * str = “hello”; for(int j=0; j<5; j++) cout << *(str+j) << endl;}
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;}
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;
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)
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;}
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;}
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;}
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
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;
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
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;
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 }
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}