jeudi 31 janvier 2019

Is it legal to pass a pointer to a function expecting an array? [duplicate]

This question already has an answer here:

Consider the code:

double func(double a[3])
{
    /* do something with a and return */
}

int main(void)
{
    std::vector<double> test(LARGE_NUM_DIVISIBLE_BY_3);
    for (size_t i = 0; i < LARGE_NUM_DIVISIBLE_BY_3 / 3; ++i)
    {
        test[3 * i] = /* some random double */;
        test[3 * i + 1] = /* some random double */;
        test[3 * i + 2] = /* some random double */;

        double val = func(&test[3 * i]);
    }
}

Is this defined behavior in C++11? I.e., can I pass a pointer (&test[3 * i]) to a function that expects an array (double func(double a[3]))? I know if I were to go the other way (i.e., pass an array to a function that expects a pointer), the array would decay to a pointer - does the reverse also work?

Aucun commentaire:

Enregistrer un commentaire