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.

Recommended Answers

All 2 Replies

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
*/

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. :!:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.