Thread: Function prototype declared as pointer

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Function prototype declared as pointer

    For example the problem asks me to:

    Write a complete C function named findItem, the prototype of which is given below, that returns the pointer to the first occurrence of an integer value item (specified as a parameter) in an array list. If the value item is not found in the array, the function returns the null pointer. Both the array and its size are specified as parameters.

    The solution is:
    Code:
    int * findItem(int * list, int size, int item)
    {
      int i;
      for (i = 0; i < size; i ++)
      if (*(list + i) == item)
      return list + i;
      return NULL;
    }
    First of all, why would there be a * in front of findItem?

    Secondly, why would some the arguments are passed as pointers while others have not? It seems counterintuive that the whole function is a pointer but the arguments are not.

    Lastly, the correct solution given above automatically returns NULL if item has not been found. How is this NULL related to the NULL pointer in the instruction?

    Can somebody explain to me how a function prototype that has been declared as a pointer work? Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hybodus
    First of all, why would there be a * in front of findItem?
    The findItem function returns a pointer to int.

    Quote Originally Posted by Hybodus
    Secondly, why would some the arguments are passed as pointers while others have not?
    The parameter named list is presumably a pointer to the first element of an array. It is a pointer since arrays are converted to pointers to their first elements when passed as arguments. size and item are not changed in the function, so there is no need to pass pointers to them.

    Quote Originally Posted by Hybodus
    Lastly, the correct solution given above automatically returns NULL if item has not been found. How is this NULL related to the NULL pointer in the instruction?
    NULL is a null pointer constant.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM