I think pointers and arrays are equivalent: but I do not understand why I get different results:
Code:
#include <iostream>
using namespace std;
void entry(short*p,short size){
int i=0;
for(short*a=p;a<p+size;++a){
cout<<"\t Enter the value of "<<i<<" : ";
cin >>*a;
cout<<"Value of a: "<<a<<"\t Address of a["<<i<<"]: "<<&a[i]<<"\t Value of a["<<i<<"]: "<<a[i]<<endl;
cout<<"\t Value of *a: "<<*a<<endl;
i++;
}
}
void print(short p[],short size){
for(short i=0;i<size;i++)
cout<<"\t value of the case "<<i<<" : "<<p[i]<<endl;
}
int main() {
cout << "Start Program" << endl;
short T1,T2;
cout<<"Give the value of T1:";
cin >>T1;
cout<<"Give the value of T2:";
cin >>T2;
short a[T1],b[T2];
cout<<"Enter table a:"<<endl;
entry(a,T1);
cout<<"Enter table b:"<<endl;
entry(b,T2);
cout<<"View table a: "<<endl;
print(a,T1);
cout<<"View table b: "<<endl;
print(b,T2);
return 0;
The Excution gives:
Start Program
Give the value of T1:2
Give the value of T2:3
Enter table a:
Enter the value of 0 : 11
Value of a : 0x23ff30 address of a[0]: 0x23ff30 Value of a[0]: 11
Value of *a: 11
Enter the value of 1 :
22
Value of a : 0x23ff32 address of a[1]: 0x23ff34 Value of a a[1]: 148 // please consider these addresses are different?
Value of *a: 22
Enter table b:
Enter the value of 0 : 55
Value of a : 0x23ff10 address of a[0]: 0x23ff10 Value of a[0]: 55
Value of *a: 55
Enter the value of 1 : 66
Value of a : 0x23ff12 address of a[1]: 0x23ff14 Value of a[1]: -216
Value of *a: 66
Enter the value of 2 : 44
Value of a : 0x23ff14 address of a[2]: 0x23ff18 Value of a[2]: 0
Value of *a: 44
View table a:
Value of case 0 : 11
Value of case 1 : 22
View table b:
Value of case 0 : 55
Value of case 1 : 66
Value of case 2 : 44
Bookmarks