Is this notation possible?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Is this notation possible?

 
0
  #1
Feb 14th, 2006
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,415
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Is this notation possible?

 
0
  #2
Feb 14th, 2006
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:
  1. double *f()
Possible usage:
  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. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Is this notation possible?

 
0
  #3
Feb 14th, 2006
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.

  1. double *f()
Possible usage:
Thanks for the example Dave. :!:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC