Thread: Interview question

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Interview question

    Hi folks
    i faced this interview question in Bosch... they asked me to write prototype fora function that returns pointer to a function that returns integer

    please let me know the prototype for above question..

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    If this is an interview question, then you should probably at least put in some effort to solve it yourself. I'm not stopping anyone else from giving you the answer, but I'd suggest reading some tutorials and at least giving it a go (and posting the result if you have trouble).

    There are a couple of decent function pointer tutorials online, and they aren't really that difficult if you are familiar with both functions and pointers.

    The Function Pointer Tutorials - Index

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Google "C Right-Left rule".
    Last edited by Overworked_PhD; 04-29-2010 at 10:09 AM.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    @DeadPlanet
    Thanks for the link you provided.. i hope below prototype is correct ..

    function returning pointer to a function with two int arguments that returns int

    int (*pfunc(int ))(int, int)
    Last edited by vijay s; 04-29-2010 at 01:27 PM.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Not quite, can you explain why you are doing:



    (*pfunc(int)) /*what is the point of (int) here? */

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vijay s
    Thanks for the linl you provided.. i hope below prototype is correct ..
    Instead of hoping, check. It looks like you do not want pfunc to take any arguments, so you should actually write:
    Code:
    int (*pfunc(void))(int, int);
    Frankly, I would just reach for a typedef:
    Code:
    typedef int (*Function)(int, int);
    
    Function pfunc(void);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    That works as well, and probably preferred.

    Typedef

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c Interview question.
    By bkankur in forum C Programming
    Replies: 3
    Last Post: 12-10-2009, 07:53 AM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  4. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM