Results 1 to 4 of 4

Thread: Are pointers and arrays equivalent

  1. #1
    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:

    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. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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. #4
    Join Date
    Feb 2008
    Posts
    1,852

    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.

Similar Threads

  1. Cursor Pointers changes In Linux
    By Koyana M. in forum Operating Systems
    Replies: 3
    Last Post: 30-12-2010, 09:04 AM
  2. What are the advantages of pointers?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 12:39 PM
  3. Pointers in C and C++
    By unlimitedtech in forum Guides & Tutorials
    Replies: 4
    Last Post: 04-02-2010, 12:42 PM
  4. Pointers VS Arrays in C++
    By Rum in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 02:06 PM
  5. Arrays and Pointers in C
    By Ivy00 in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 05:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,690,044.07878 seconds with 17 queries