TechArena Community Are pointers and arrays equivalent

#1
16-10-2009
 Member Join Date: Sep 2009 Posts: 125
Are pointers and arrays equivalent

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:

Quote:
 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
#2
16-10-2009
 Member Join Date: Jan 2008 Posts: 1,515
Re: Are pointers and arrays equivalent

You are getting different answers because of a[1] == a + 1 * sizeof(short) (a[2] == a + 2 * sizeof(short), etc)

Also you have increments in your loop. There is no need for a[i], it results in repetition of statements.
#3
16-10-2009
 Member Join Date: Oct 2005 Posts: 2,389
Re: Are pointers and arrays equivalent

an array is like:

Type var[Size];

a pointer is like:
Type *var;

The only equivalence that exists is that the name of an array is also a pointer to its element 0.

Be careful not to mix everything
#4
16-10-2009
 Member Join Date: Feb 2008 Posts: 1,848
Re: Are pointers and arrays equivalent

There is no equivalence between pointers and arrays in C and C++. There are a number of syntactic and semantic features that maintain the
confusion

- In defining the parameters of a function, a parameter of type array is adjusted in a parameter of type pointer;
- In many contexts (exceptions are the C sizeof arguments and & in C++ there are more, related to resolution of overload and the deduction of template arguments), a value array type is implicitly converted into a pointer to its first element. Unlike was what some people think.

 Tags: