Function declarations

This is a discussion on Function declarations within the C Programming forums, part of the General Programming Boards category; Hi, Quick question. Was looking in to some of the complex function declaration topic and came across this question. I ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Function declarations

    Hi, Quick question. Was looking in to some of the complex function declaration topic and came across this question. I have two answers for this. Just wanted to make sure that I am right.

    Here is the question which i came across, where i need to write the function declaration for it.

    Declare a pointer to a function taking an int and a pointer to int and returning a pointer to an array of pointer to char.
    And here are my two solution

    Solution 1
    Code:
    char ** (*f)(int, int *);
    Solution 2
    Code:
    char *[] (*f)(int, int *);
    I am confused, when it says "pointer to an array of pointer to char". Will this be a pointer to a pointer as well.

    Is my understanding of that question is right. Please put me in a right direction.

    Thanks a lot

    ssharish

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yeah, that's the part that had me scratching my head too...
    I think it's just very badly written. The way it's written makes it sound like they want a char*** returned; but since triple pointers are extremely rare, I'd say it's just asking for a char**.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Sounds like a triple char pointer to me.

    ...a pointer to an array of pointer to char.
    Going backwards.....

    char *
    char **
    char ***

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Right understood now, the mistake which i was doing was solving that through left to right. Thanks a lot for both of you for a quick reply.

    Is there any application of using that sort of Declaration. Just curious to know . Have u any found come across using just a function declaration.

    ssharish

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You need function pointers for DLLs if you are going to dynamically load and use a DLL (at runtime, of course).

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Sorry to bug u all again. I have one more question. I came across this question as well. And I have solved it as well. Just tell me am I right. This is the question

    Declare a function which takes an array of function pointers and returns a function pointer. All function pointers are to functions which take an int and return a pointer to char.
    Solution
    Code:
    char * ( *fun( *[] ) )( int );

    You need function pointers for DLLs if you are going to dynamically load and use a DLL (at runtime, of course).
    OK it would be normally used int developing libraries. In think OpenGL uses a lot ofd those isnt?

    Thank you

    ssharish

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This is kind of messy, and I'm not checking my work, so this might be wrong..... but my solution would be something like this in real life:

    Code:
    typedef char *(*fn)(int) fnptr;
    fnptr somefunction(fnptr *);
    Expanding it and getting rid of the typedef...:

    Code:
    (char *(*fn)(int)) somefunction((char *(*fn)(int)) *);
    Yeah....

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    why do i get tis error message on typedef. It looks to me everything is fine

    Code:
    typedef void (*(*Bloggs)[10])(int **) FTR;
    FTR fred(int *, const char *);
    Error message
    17 D:\Final year\Distributed Systems\Tutorial1\Prog2.c expected '=', ',', ';', 'asm' or '__attribute__' before 'FTR'
    18 D:\Final year\Distributed Systems\Tutorial1\Prog2.c expected '=', ',', ';', 'asm' or '__attribute__' before 'fred'

    ssharish

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    You can't typedef an array type, I believe. You would have to break it down further. Perhaps a pointer to a function with int ** as a parameter and returning a void *.

    Then you could return an array of function pointers like you would any other.

    typedef void *( *foo )( int ** );
    foo *bar( int *d, const char *s );

    Making a typedef for a function pointer is something to get used to for sure. Notice I only had to name the pointer.

    whoops.
    Last edited by whiteflags; 10-09-2007 at 12:22 AM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You can't typedef an array type, I believe.
    Sure you can. You can typedef pretty much anything:
    Code:
    #include <stdio.h>
    
    typedef int i10[10];
    
    int main ( void )
    {
      i10 a;
      int i;
    
      for ( i = 0; i < 10; i++ )
        a[i] = i;
    
      for ( i = 9; i >= 0; i-- )
        printf ( "%d\n", a[i] );
    
      return 0;
    }
    >why do i get tis error message on typedef.
    Because Bloggs is the name of the typedef, not FTR. Your compiler doesn't know how to parse FTR. Try this instead:
    Code:
    typedef void (*(*Bloggs)[10])(int **);
    Bloggs fred(int *, const char *);
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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