Function prototype declared as pointer

This is a discussion on Function prototype declared as pointer within the C Programming forums, part of the General Programming Boards category; For example the problem asks me to: Write a complete C function named findItem, the prototype of which is given ...

  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
    21,390
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:03 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21