Divyanshu Agarwal
Divyanshu Agarwal

Reputation: 19

Can we somehow use array[i] instead of *(array+i)?

In C++, when I have to use an array inside some function, I pass the array as an argument and get a pointer pointing to the first element of the array. While it is okay to use, and not much of a hassle to use a pointer, I was wondering if there exists some in-built header file, or any other set of instructions, by which when I want to access the i-th element of the array I can simply write array[i] and it gets read by the compiler as *(array+i)?

It would be great if one exists, since it would make it quite uniform and easy to code, since all the times when I use vectors I can access the i-th element just by vector[i] while in array I have to use it the other way *(array+i).

Also, is there some reason why the developers of C++ chose to return pointers to an array instead of the object itself?

Upvotes: 0

Views: 609

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

From the C++ 14 Standard (5.2.1 Subscripting)

1 A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “array of T” or “pointer to T” and the other shall have unscoped enumeration or integral type. The result is of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7 for details of * and + and 8.3.4 for details of arrays. — end note ], except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise.

So these expressions array[i] and *( array + i ) are evaluated the same way whether array in this expressions is an array designator or a pointer to first element of an array,

Moreover these expressions array[i] and i[array] are also evaluated the same way.

Arrays are non-modifianle lvalues so you may not return an array from a function. If to use an array designator in a return statement it will be converted to pointer to its first element. As a result arrays do not have the copy assignment operator.

On the other hand you can return a reference to an array provided that it doesn not have automatic storage duration.

For example

#include <iostream>

const size_t N = 5;

decltype( auto ) f( int ( &a )[N], int init )
{
    for ( size_t i = 0; i < N; i++ )
    {
        a[i] = init++;
    }
    
    return a;
}

int main() 
{
    int a[N];
    
    decltype( auto ) ra = f( a, 0 );
    
    std::cout << sizeof( ra ) << '\n';
    
    for ( const auto &item : ra )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output is

20
0 1 2 3 4

Upvotes: 0

eerorika
eerorika

Reputation: 238401

Can we somehow use array[i] instead of *(array+i)?

Yes, we can. Those expressions are practically identical. The subscript operator is much more readable, so I recommend using that.

I was wondering if there exists some in-built header file

You don't need to include any header.

while in array i have to use it the other way *(array+i)

Just because you can, doesn't mean that you have to. You don't have to use it the other way.

P.S. Besides arrays and vectors, we can also use the subscript operator with a pointer to element of an array.


Also, is there some reason why the developers of C++ chose to return pointers to an array instead of the object itself?

Because sometimes indirection is necessary or useful.

Upvotes: 1

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275740

I was wondering if there exists some in-built header file or any other set of instructions by which when I want to access the i-th element of the array I can simply write array[i] and it gets read by the compiler as *(array+i)

No, there is no such header or set of instructions, because it is part of the language.

For a pointer and an integral type, a[i] means *(a+i). This is such a strong statement that:

int base_array[3]={1,2,3}; // an array of 3 elements
int* ptr_array = base_array; // base_array "decays" to a pointer to the first element
std::cout << 2[ptr_array] << "\n"; // huh?!?!

prints 3, because *(2+ptr_array) is 3; ie, it even works backwards.

Upvotes: 6

Brian Bi
Brian Bi

Reputation: 119382

If a is a pointer and i has an integral type, then a[i] is always the same as *(a+i). There is no need to include a header or anything to make it work.

Upvotes: 12

Related Questions