Pointers - * operator

We use the * operator to obtain and modify the value of the variable being pointed to by the pointer however, we don't use this operator while making arrays using dynamic allocation.Why is it so ?
Ex.
1
2
3
4
5
6
7
8

int * val ;
val = new int ;
cout<<"enter a value : ";
cin>>*val ;                             //Here we use *   
cout<<"The value entered is :"<<*val;  
delete val;


1
2
3
4
5
6
7
8
9
10

int *val ,r;
cout<<"Enter size of array :"; cin>>r;
cout<<"Enter element of array : ";
for(int i=0 ; i<r ; i++)
cin>>val[r];                                 //Here * is not used . 
cout<<"enter elements of array are :";
for(int i=0 ; i<r ; i++)
cout<<val[r]<<" ";                            //Again * is not used .
Last edited on
Line 3 in your first example is incorrect, you do not want the * there.

In your second example, we are in fact actually using *:

1
2
3
val[x];
//is the same as
*(val+x);
OK ! I understood ....Thanks !
Topic archived. No new replies allowed.