954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is this notation possible?

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.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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. :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You