Today we shall learn about pointers in c++.Pointers are those which are stores the address of a variable and can access those variables using these pointers. The main condition of pointers is that they should be of same data type as that of variable which you are going to assign to it. pointers can be applied to both a single variable or for an array. Pointers can be operated using two operators ampersand '&' and by asterisk '*'.
In C++ we have classes which contain data member and member functions.We can assign pointers to data members and member functions. We can also assign pointers to these members of a class.Let's learn how to apply pointers to individual variables,to arrays,member of class.
To individuals: Let us consider we have declared a variable of type int. Now we need to assign pointer to this integer variable.Assignment of pointer to variables implies that the pointer need to store the address of the variable. This assignment is done by ampersand operator as follows:
void main()
{
int a;
int *p;
p=&a;
}
Example show the assignment of pointer to a variable.
We have assigned a pointer to that variable.Now we need to access that variable using the pointer, this is done by asterisk operator. Let us think that we need to assign an pointer to a variable and need to display the value of the variable using pointer:
void main()
{
int a;
cout<<"Enter a value:";
cin>>a;
int *p;
p=&a;
cout<<"Entered a values is:"<<*p;
}
To array of variables: Now we shall assign pointers to array of variables.Consider that we have created an array of data types int.Now we need to create a pointer,assign pointer and access that array using pointer. we shall now write a program and explain it.
void main()
{
int array[20];
int *p;
for(i=0;i<20;i++)
{
cout<<"Enter"<<i<<"Value:";
cin>>array[i];
}
int *p;
p=array;
(or)
p=&array[i];
for(i=0;i<20;i++)
{
cout<<"Entered values are"<<*p;
p++;
}
}
Here we assigned array pointer in two ways.One by assigning just array to the pointer and other by specifying the address of the starting index.While displaying the array values we are using pointer and we need to increment pointer from one position to other.By general increment i.e a++ a is incremented by one,but here we need to increment pointer from one memory cell to other that increment depends upon datatype of the variable.Therefore we need to specify datatype of pointer same as that's of variable.
In C++ we have classes which contain data member and member functions.We can assign pointers to data members and member functions. We can also assign pointers to these members of a class.Let's learn how to apply pointers to individual variables,to arrays,member of class.
To individuals: Let us consider we have declared a variable of type int. Now we need to assign pointer to this integer variable.Assignment of pointer to variables implies that the pointer need to store the address of the variable. This assignment is done by ampersand operator as follows:
void main()
{
int a;
int *p;
p=&a;
}
Example show the assignment of pointer to a variable.
We have assigned a pointer to that variable.Now we need to access that variable using the pointer, this is done by asterisk operator. Let us think that we need to assign an pointer to a variable and need to display the value of the variable using pointer:
void main()
{
int a;
cout<<"Enter a value:";
cin>>a;
int *p;
p=&a;
cout<<"Entered a values is:"<<*p;
}
To array of variables: Now we shall assign pointers to array of variables.Consider that we have created an array of data types int.Now we need to create a pointer,assign pointer and access that array using pointer. we shall now write a program and explain it.
void main()
{
int array[20];
int *p;
for(i=0;i<20;i++)
{
cout<<"Enter"<<i<<"Value:";
cin>>array[i];
}
int *p;
p=array;
(or)
p=&array[i];
for(i=0;i<20;i++)
{
cout<<"Entered values are"<<*p;
p++;
}
}
Here we assigned array pointer in two ways.One by assigning just array to the pointer and other by specifying the address of the starting index.While displaying the array values we are using pointer and we need to increment pointer from one position to other.By general increment i.e a++ a is incremented by one,but here we need to increment pointer from one memory cell to other that increment depends upon datatype of the variable.Therefore we need to specify datatype of pointer same as that's of variable.
letusprogram.wordpress.com
ReplyDelete