Thread: doubt on function pointer using typedef

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    doubt on function pointer using typedef

    As per my understanding following is the syntax for function pointer using typedef.


    Code:
    typedef void (*fp)(int arg);
    I saw typedef to function definition similar to below.


    Code:
    typedef int func(int arg);
    func *fa[] = {
        [0] = f1,
        [1] = f2,
        [2] = f3,
        [3] = f4,
        [4] = f5,
    };
    Is it array of function pointers and the typedef declaration in the above code snippet is a function pointer or different.

    I got the dobht after seeing the seeing the declarations @
    git.denx.de Git - u-boot.git/blob - common/cmd_bootm.c

    git.denx.de Git - u-boot.git/blob - common/cmd_bootm.c

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It's basically a function declaration without the pointer part. Notice the star in "func *fa[] = ...". So it an array of pointers to func.
    It's also used to just declare functions using "variable style syntax":
    Code:
    static boot_os_fn do_bootm_vxworks;
    static boot_os_fn do_bootm_qnxelf;
    gg

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    I couldn't understand
    "So it an array of pointers to func.
    It's also used to just declare functions using "variable style syntax":"
    Can you please elaborate a bit.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    typedef void (*fp)(int arg);
    This is typedef of a pointer to a function fp.

    typedef void f(int arg);
    This is a typedef of function f (no pointer).
    Code:
    f foo; // declaration of a function foo
    fp pfoo; // definition of a function pointer pfoo
    f *pfoo2; // definition of function pointer pfoo2
    
    void foo(int arg) {} // definition of function foo
    
    pfoo = pfoo2 = &foo; // both point to foo()
    gg

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Personally, if it was my program, I just wouldn't typedef the pointer to a function (IMO it makes things confusing and hard to read)

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I find not typedefing function pointers to be silly - they aren't used like normal pointers, otherwise you might have a point there. No matter what function-like thing foo is, foo() is an invocation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function pointer doubt
    By Satya in forum C Programming
    Replies: 2
    Last Post: 07-04-2015, 10:17 PM
  2. Function Pointer Typedef with Same Type Arguments
    By SevenThunders in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2015, 02:55 AM
  3. typedef a function pointer question..
    By MegaManZZ in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2008, 10:57 AM
  4. Typedef representing pointer to function
    By pdc in forum C Programming
    Replies: 3
    Last Post: 06-10-2005, 10:10 AM
  5. why typedef? and not a pointer to function?
    By terracota in forum Windows Programming
    Replies: 10
    Last Post: 12-19-2004, 06:22 PM

Tags for this Thread