-1

There's a strange code here:

const double a[] = {0,1,2,3,4};
int main()
{
    double *p = a;
    printf("%f\n",p[2]); //2.000000
    printf("%f\n",p);    //2.000000
}

It return 2.000000 ,why?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

6

The code

printf("%f\n",p);

causes undefined behavior. To print an address (a pointer) which is the type an array name decays to when passed as function argument, you need to

  • use %p conversion specifier.
  • cast the argument to (void *).
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261