0

Hello ladies and gents,

There's an exercise, well, more a question that goes like this in Accelerated C++:

If the following code is legal, what can we infer about the return type of f?

double d = f() [n];

What I can gather is that the return type will be a double variable, but, I don't know wether this way of writing is actually correct?

Ive tried to use it, but failed in getting anything to work with this kind of function.

Could someone tell me wether this is a legal piece of code.

Thanks.

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by JoBe
0

If the following code is legal, what can we infer about the return type of f?

That it returns a pointer to a double:

double *f()

Possible usage:

#include <iostream>

double *f()
{
    static double value[] = {1,2,3,4,5};
    return value;
}

int main ( void )
{
    int n = 2;
    double d = f() [n];
    std::cout << "d = " << d << '\n';
    return 0;
}

/* my output
d = 3
*/
0

That it returns a pointer to a double...

Darn, should have seen it, because of f() [n] :confused: It couldn't have been a double.

double *f()

Possible usage:

Thanks for the example Dave. :!:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.