Thread: Function pointer doubt

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Function pointer doubt

    I was doing pointer to function program from example and made my version of the program.
    Code:
    #include<stdio.h>
    
    int testfunction(int);
    
    void testfunction1(void);
    
    int (*comp)(int);
    
    int passfunciton(int (*)(int));
    
    int main(int argc, char *argv[])
    {
     passfunction((int(*)(int))(testfunction1));
     passfunction(testfunction1);
     
     return 0;
    }
    int testfunction(int var)
    {
        printf("%d",var);
    }
    
    int passfunction(int (*comp)(int))
    {
        (*comp)(15);
    }
    
    void testfunction1(void)
    {
        printf("%d\n",16);
    }
    The first passfunction is from example with some modifications. Does the
    Code:
    (int(*)(int)) is for type casting?
    I removed that even then it compiles without warning. My main doubt is testfunction1 is of different type even then it compiles without warnings. Why?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If it compiles without errors or warnings for you it is because you have your compiler warning levels too low. Here are the warnings and errors I got when compiling your code:

    main.c||In function ‘main’:|
    main.c|13|error: implicit declaration of function ‘passfunction’ [-Wimplicit-function-declaration]|
    main.c|11|warning: unused parameter ‘argc’ [-Wunused-parameter]|
    main.c|11|warning: unused parameter ‘argv’ [-Wunused-parameter]|
    main.c|23|warning: no previous declaration for ‘passfunction’ [-Wmissing-declarations]|
    main.c||In function ‘passfunction’:|
    main.c|23|warning: declaration of ‘comp’ shadows a global declaration [-Wshadow]|
    main.c|7|warning: shadowed declaration is here [-Wshadow]|
    main.c||In function ‘testfunction’:|
    main.c|21|warning: control reaches end of non-void function [-Wreturn-type]|
    main.c||In function ‘passfunction’:|
    main.c|26|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 1 errors, 7 warnings (0 minutes, 0 seconds) ===|
    Jim

  3. #3
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Here's what I get via GCC:
    Code:
    main.c: In function 'main':
    main.c:20:3: warning: implicit declaration of function 'passfunction' [-Wimplicit-function-declaration]
       passfunction((int(*)(int))(testfunction1));
       ^
    main.c:18:14: warning: unused parameter 'argc' [-Wunused-parameter]
     int main(int argc, char *argv[])
                  ^
    main.c:18:26: warning: unused parameter 'argv' [-Wunused-parameter]
     int main(int argc, char *argv[])
                              ^
    main.c: In function 'testfunction':
    main.c:28:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    main.c: In function 'passfunction':
    main.c:33:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    It barks about the return because I didn't compile with C99 or anything. This is what C89 would say about your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer doubt
    By ankur0691 in forum C Programming
    Replies: 3
    Last Post: 06-13-2013, 11:14 AM
  2. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  3. pointer doubt....
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 11:20 AM
  4. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  5. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM