0

Simple example:

double array[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p1 = array;
printf("p1: %f\n", *(p1));

int x = 2;
int* p2 = &x;
printf("p2: %d\n", *p2);

why on p1 I need to use & and on array I don't need this in c/c++? Is there a logic reason?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
markzzz
  • 47,390
  • 120
  • 299
  • 507

4 Answers4

1

An array designator in expressions is converted (with rare exception) to pointer to its first element.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

And from the C++ Standard (4.2 Array-to-pointer conversion)

1 An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

Thus this declaration

double *p1 = array;

is equivalent to

double *p1 = &array[0];
            ^^^

Consider this demonstrative program

$include <iostream>

int main()
{
    double array[] = { 1000.0, 2.0, 3.4, 17.0, 50.0 };

    std::cout << "sizeof( array ) = " << sizeof( array ) << std::endl;
    std::cout << "sizeof( array + 0 ) = " << sizeof( array + 0 ) << std::endl;
}    

The program output is

sizeof( array ) = 40
sizeof( array + 0 ) = 8

In the first output statement the array designator is used as an operand of the sizeof operator. So there is no conversion from the type double[5] to double *.

In the second output statement the array designator is used in expression array + 0 that in turn is used as an operand of the sizeof operator. In this case there is conversion from the type double[5] to the type double *.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You can think that array evaluates to a variable of type 'address' while x is a variable of type int. x is stored in a memory location and &x gives you the address of that memory location.

The address of the array, that is &array will evaluate to the address of the first element and hence the address of the array itself.

Sid
  • 7,511
  • 2
  • 28
  • 41
0

According to Bjarne Stroustrup's book - Programming: Principles and Practice Using C++ name of an array refers to all elemnts of the array, so for example:

char ar[10];
sizeof(ar) // 10

However, the name of an array turns into ("decays to") a pointer with the slightest excuse.

So this is why we can have: char* c = ar. Now c is initialized to &c[0] (that is first element of an array) making sizeof(c) 4 (depending of implementation.

As for variable it is just one element.

sebap123
  • 2,541
  • 6
  • 45
  • 81
0

Because an array decays to a pointer of it's declared type that points to the first element that the array holds.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122