Calling a function using its pointer-

Discussion in 'C Programming' started by Samie, Aug 31, 2004.

  1. Samie

    Samie Guest

    Hi
    I have declared a structure that contains pointer to a function. The
    code goes like this:
    typedef struct lcd_funs lcd_funs;
    struct lcd_funs {
    void (*decode_image)(cyg_uint32 imageWidth,
    cyg_uint32 imageHeight,
    Palette_element *paletteData,
    cyg_uint8 *imageData,
    cyg_uint16 bitsPerPixel
    );
    };

    Now, suppose, I have a variable funs that is pointer to a structure of
    this type. When I try to call this function using the code below, it
    hangs up. Any idea why it is happening like this? The code used to
    call it as follows:
    (funs->decode_image)(ptrConfigOptions->imageWidth,
    ptrConfigOptions->imageHeight,
    ptrConfigOptions->paletteData,
    ptrConfigOptions->imageData,
    ptrConfigOptions->bitsPerPixel
    );
    Is it the right way to call functions using their pointers that are
    members of a structure?

    Regards
    Samie
    Samie, Aug 31, 2004
    #1
    1. Advertising

  2. Samie

    -berlin.de Guest

    Samie <> wrote:
    > I have declared a structure that contains pointer to a function. The
    > code goes like this:
    > typedef struct lcd_funs lcd_funs;
    > struct lcd_funs {
    > void (*decode_image)(cyg_uint32 imageWidth,
    > cyg_uint32 imageHeight,
    > Palette_element *paletteData,
    > cyg_uint8 *imageData,
    > cyg_uint16 bitsPerPixel
    > );
    > };


    > Now, suppose, I have a variable funs that is pointer to a structure of
    > this type. When I try to call this function using the code below, it
    > hangs up. Any idea why it is happening like this? The code used to
    > call it as follows:
    > (funs->decode_image)(ptrConfigOptions->imageWidth,
    > ptrConfigOptions->imageHeight,
    > ptrConfigOptions->paletteData,
    > ptrConfigOptions->imageData,
    > ptrConfigOptions->bitsPerPixel
    > );
    > Is it the right way to call functions using their pointers that are
    > members of a structure?


    Yes, but you don't need the parentheses around the 'funs->decode_image'
    bit. Why it hangs is a different question. First possibility to get
    it wrong is not assigning the correct pointer to the 'decode_image'
    member of the structure. Second possibility is that the function isn't
    working as you expect it to. Did you try to use a debugger to see if
    the function gets called at all (e.g. by setting a breakpoint at the
    very start of it) and then to see what happens inside of it?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 31, 2004
    #2
    1. Advertising

  3. Samie

    Eric Sosman Guest

    Samie wrote:
    > Hi
    > I have declared a structure that contains pointer to a function. The
    > code goes like this:
    > typedef struct lcd_funs lcd_funs;
    > struct lcd_funs {
    > void (*decode_image)(cyg_uint32 imageWidth,
    > cyg_uint32 imageHeight,
    > Palette_element *paletteData,
    > cyg_uint8 *imageData,
    > cyg_uint16 bitsPerPixel
    > );
    > };
    >
    > Now, suppose, I have a variable funs that is pointer to a structure of
    > this type. When I try to call this function using the code below, it
    > hangs up. Any idea why it is happening like this? The code used to
    > call it as follows:
    > (funs->decode_image)(ptrConfigOptions->imageWidth,
    > ptrConfigOptions->imageHeight,
    > ptrConfigOptions->paletteData,
    > ptrConfigOptions->imageData,
    > ptrConfigOptions->bitsPerPixel
    > );
    > Is it the right way to call functions using their pointers that are
    > members of a structure?


    The call looks all right to me. Have you actually
    set the `decode_image' element to point at a function
    of the appropriate type?

    void decode_jpeg_image( /* prototype here */ );
    void decode_tiff_image( /* prototype here */ );
    ...
    if (jpeg_rulez)
    funs->decode_image = decode_jpeg_image;
    else
    funs->decode_image = decode_tiff_image;
    ...
    funs->decode_image( /* argument values here */);

    If `decode_image' is in fact pointing to the function
    you expect (you could insert a printf() at the start of
    that function to be sure you get there), then your "it
    hangs up" problem has some other cause.

    --
    Eric Sosman, Aug 31, 2004
    #3
  4. Samie

    Buzzard Guest

    before you use the call funs->decode_image,you must do this as follow:
    funs=(lcd_funs *)malloc(sizeof(lcd_funs));

    Samie <> wrote in message
    news:...
    > Hi
    > I have declared a structure that contains pointer to a function. The
    > code goes like this:
    > typedef struct lcd_funs lcd_funs;
    > struct lcd_funs {
    > void (*decode_image)(cyg_uint32 imageWidth,
    > cyg_uint32 imageHeight,
    > Palette_element *paletteData,
    > cyg_uint8 *imageData,
    > cyg_uint16 bitsPerPixel
    > );
    > };
    >
    > Now, suppose, I have a variable funs that is pointer to a structure of
    > this type. When I try to call this function using the code below, it
    > hangs up. Any idea why it is happening like this? The code used to
    > call it as follows:
    > (funs->decode_image)(ptrConfigOptions->imageWidth,
    > ptrConfigOptions->imageHeight,
    > ptrConfigOptions->paletteData,
    > ptrConfigOptions->imageData,
    > ptrConfigOptions->bitsPerPixel
    > );
    > Is it the right way to call functions using their pointers that are
    > members of a structure?
    >
    > Regards
    > Samie
    Buzzard, Sep 1, 2004
    #4
  5. Samie

    -berlin.de Guest

    Buzzard <> wrote:
    > Samie <> wrote in message
    > news:...
    >> Hi
    >> I have declared a structure that contains pointer to a function. The
    >> code goes like this:
    >> typedef struct lcd_funs lcd_funs;
    >> struct lcd_funs {
    >> void (*decode_image)(cyg_uint32 imageWidth,
    >> cyg_uint32 imageHeight,
    >> Palette_element *paletteData,
    >> cyg_uint8 *imageData,
    >> cyg_uint16 bitsPerPixel
    >> );
    >> };
    >>
    >> Now, suppose, I have a variable funs that is pointer to a structure of
    >> this type. When I try to call this function using the code below, it
    >> hangs up. Any idea why it is happening like this? The code used to
    >> call it as follows:
    >> (funs->decode_image)(ptrConfigOptions->imageWidth,
    >> ptrConfigOptions->imageHeight,
    >> ptrConfigOptions->paletteData,
    >> ptrConfigOptions->imageData,
    >> ptrConfigOptions->bitsPerPixel
    >> );
    >> Is it the right way to call functions using their pointers that are
    >> members of a structure?


    Please don't toppost.

    > before you use the call funs->decode_image,you must do this as follow:
    > funs=(lcd_funs *)malloc(sizeof(lcd_funs));


    Not necessarily. If the structure is defined and initialized e.g. in
    some function and then a pointer to it is passed to some other function,
    where funs->decode_image() is called, no dynamic allocation is required.
    BTW, even if an allocation would be needed the cast of the return
    value of malloc() is superfluous, it will just keep the compiler from
    complaining if you forgot to include <stdlib.h>.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Sep 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    453
    Mike Wahler
    Jan 10, 2005
  2. weafon
    Replies:
    1
    Views:
    281
    Diez B. Roggisch
    Jul 14, 2009
  3. thunk
    Replies:
    1
    Views:
    286
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    444
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    588
    thunk
    Apr 3, 2010
Loading...

Share This Page