944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1854
  • C++ RSS
Feb 14th, 2006
0

Is this notation possible?

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. 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.
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 14th, 2006
0

Re: Is this notation possible?

Quote originally posted by JoBe ...
If the following code is legal, what can we infer about the return type of f?
That it returns a pointer to a double:
C++ Syntax (Toggle Plain Text)
  1. double *f()
Possible usage:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. double *f()
  4. {
  5. static double value[] = {1,2,3,4,5};
  6. return value;
  7. }
  8.  
  9. int main ( void )
  10. {
  11. int n = 2;
  12. double d = f() [n];
  13. std::cout << "d = " << d << '\n';
  14. return 0;
  15. }
  16.  
  17. /* my output
  18. d = 3
  19. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 14th, 2006
0

Re: Is this notation possible?

Quote originally posted by Dave Sinkula ...
That it returns a pointer to a double...
Darn, should have seen it, because of f() [n] It couldn't have been a double.

Quote ...
C++ Syntax (Toggle Plain Text)
  1. double *f()
Possible usage:
Thanks for the example Dave. :!:
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: assigning a string from a file to a variable
Next Thread in C++ Forum Timeline: string: get token from string and compare token from text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC