Thread: What's the purpose of declaring function as pointers?

  1. #1
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16

    What's the purpose of declaring function as pointers?

    Hi Mentors,

    I am bit confused as why does one need to declared the functions as pointers.

    For example I was going through fgets functions which has the following prototype defined in stdio.h

    char *fgets(char *str, int size, FILE* file);

    Please provide your valuable inputs.

    Thanks
    Ankit

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's not an example of a function pointer. What 'fgets' does is return a char pointer.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    For starters, the arguments of fgets() aren't function pointers. Look at the signal() syscall for an example of function pointers and it'll also tell you why 'n where they are applicable.

  4. #4
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by nonoob View Post
    That's not an example of a function pointer. What 'fgets' does is return a char pointer.
    but what does char *fgets () mean ?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    means fgets returns a string.
    You can do
    Code:
    char *pnt;
    ...
    pnt = fgetc(buffer, 100, stream); /* returns a string */
    printf("String is %s\n", pnt);
    I'm not sure why one would want to look at that string if the buffer variable is designed to contain it. But there are times when the returned pointer is NULL - some error occurred.
    Last edited by nonoob; 02-03-2012 at 02:57 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonoob View Post
    means fgets returns a string.
    No, it means that fgets() returns a pointer to char.

    A string is something different from a pointer. By convention, however, if fgets() returns non-NULL, the data at that address is a collection of contiguous char's, with the last one zero (which is how strings are represented in the C standard library).

    fgets() returns a NULL pointer if an error occurs or, if no characters have been read, the end of the stream has been reached.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ankiit View Post
    I am bit confused as why does one need to declared the functions as pointers.
    A function can't really be declared as a pointer, but you can declare a pointer to a function. That would be a "function pointer", but as a few people have said, this isn't one.

    All functions have a type; it may be any type, but it must be the same type as the value returned by the function. If the function does not return anything, its type should be void. Note that this is not the same as void*; a void function returns nothing, but a void* function returns a pointer to a void type.

    The type of fgets() is char*, because fgets returns a pointer to a char type.

    A char pointer may point to the beginning of a string. In practice, it almost always is.
    Last edited by MK27; 02-03-2012 at 03:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi ,

    I know some information about that function pointer that we can use them more in callback functions.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by nkrao123@gmail. View Post
    Hi ,

    I know some information about that function pointer that we can use them more in callback functions.
    Where did you see function pointers here ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Purpose of a function
    By arun10427 in forum C Programming
    Replies: 5
    Last Post: 09-28-2010, 11:24 AM
  2. what is the purpose of this function..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 03-25-2009, 02:03 AM
  3. What is the purpose of this function argument?
    By dudeomanodude in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2008, 06:59 PM
  4. Declaring Pointers/Passing Data
    By Ryan_P in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2002, 02:49 PM
  5. Purpose of pointers?
    By Yawgmoth in forum C Programming
    Replies: 9
    Last Post: 12-16-2002, 06:14 PM