passing struct by reference to function

Discussion in 'C Programming' started by Pushkar Pradhan, Dec 22, 2003.

  1. I need to pass a struct by reference to a function:
    The struct:
    typedef struct tifftags {
    uint32 imageWidth, ...;
    ...
    } TIFFTAGS;
    typedef TIFFTAGS * TIFFTAGS_PTR;

    main() {
    .....
    TIFFTAGS tags;

    /* This function should update tags' members */
    readTIFFTags(file, tif, &tags);
    ...
    }

    void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {

    /* TIFFGetField should be called with the address of the variable
    which should be updated */
    if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
    CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);

    if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
    CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);

    ......
    }

    The first syntax gives compile errors:
    tiff/tiff_misc.c: In function `readTIFFTags':
    tiff/tiff_misc.c:8: request for member `imageWidth' in something not a
    structure or union
    While the second syntax (tags->imageLength) gives a seg fault. Also
    tried some other syntaxes.

    Please let me know the correct syntax. Thanks,
    Pushkar Pradhan
    Pushkar Pradhan, Dec 22, 2003
    #1
    1. Advertising

  2. Pushkar Pradhan wrote:
    >
    > typedef struct tifftags {
    > uint32 imageWidth, ...;
    > ...
    > } TIFFTAGS;
    > typedef TIFFTAGS * TIFFTAGS_PTR;
    >
    > void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {
    >
    > /* TIFFGetField should be called with the address of the variable
    > which should be updated */
    > if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
    > CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);
    >
    > if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
    > CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);
    >
    > .....
    > }
    >
    > The first syntax gives compile errors:
    > tiff/tiff_misc.c: In function `readTIFFTags':
    > tiff/tiff_misc.c:8: request for member `imageWidth' in something not a
    > structure or union
    > While the second syntax (tags->imageLength) gives a seg fault. Also
    > tried some other syntaxes.


    The first problem I see is that the variable "tags" is undeclared in
    readTIFFTags. Maybe you meant the second parameter to be "tags" instead of
    "tag"? I'll assume that's the case.

    &tags.imageWidth won't work, because tag is a pointer, not a struct--and
    therefore you can't use the . operator on it.

    I'm guessing you're having a problem with tags->imageLength because the
    function TIFFGetField expects a pointer--that's what your comment says--and
    tags->imageLength is a uint32, which I assume is not a pointer type.

    Maybe you want to say

    &tags->imageWidth

    ? "tags->imageWidth" gives you a uint32 object, and then the & operator
    gives you its address.

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, Dec 22, 2003
    #2
    1. Advertising

  3. Pushkar Pradhan

    Malcolm Guest

    "Pushkar Pradhan" <> wrote in message
    > I need to pass a struct by reference to a function:
    >

    In C you pass a pointer to the structure, which you use to update it.
    >
    > The struct:
    > typedef struct tifftags {
    > uint32 imageWidth, ...;
    > ...
    > } TIFFTAGS;
    > typedef TIFFTAGS * TIFFTAGS_PTR;
    >

    This last typedef is asking for typedef trouble.
    >
    > main() {
    > ....
    > TIFFTAGS tags;
    >
    > /* This function should update tags' members */
    > readTIFFTags(file, tif, &tags);
    > ..
    > }
    >
    > void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {
    >
    > /* TIFFGetField should be called with the address of the variable
    > which should be updated */
    > if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
    >

    This shoule be tags->imageWidth. You have typedefed your way into a
    situation where you no longer understand what is going on.
    >
    > CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);
    >
    > if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
    >

    This seems to be OK. Presuming the structure has a member "imageLength" you
    should be able to access it. However segfaults can occur for many reasons.
    Try
    tags->imageLength = 0;
    to ensure that this function is working OK, then look at the TIFFGetField
    function.
    >
    > CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);
    >
    > .....
    > }
    >

    Good luck.
    Malcolm, Dec 23, 2003
    #3
  4. "Pushkar Pradhan" <> wrote in message
    news:...
    > I need to pass a struct by reference to a function:
    > The struct:
    > typedef struct tifftags {
    > uint32 imageWidth, ...;
    > ...
    > } TIFFTAGS;
    > typedef TIFFTAGS * TIFFTAGS_PTR;
    >
    > main() {
    > ....
    > TIFFTAGS tags;


    This statement defines a pointer veriable of type TIFFTAGS or u can say
    struct tifftags.


    >
    > /* This function should update tags' members */
    > readTIFFTags(file, tif, &tags);


    Here u r passing the address of pointer variable tags (I am right in case u
    have written code in C).



    > ..
    > }
    >
    > void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {


    This function prototype says that u r receiving third argument as a pointer
    of type TIFFTAGS. Which is actually wrong it should be TIFFTAGS *tags.
    Because while passing u passed pointer to pointer.



    >
    > /* TIFFGetField should be called with the address of the variable
    > which should be updated */
    > if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )


    I dont about this statement what u r trying to do.

    > CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);
    >
    > if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )


    This is not at all possible. Because function calling and function
    definition does not match.
    Here if u want to access imageWidth value then use (*tags)->imageWidth.
    If you want its address use &((*tags)->imageWidth).
    This is possible only if u take care while receiving your third parameter.


    With Regards.
    SanBid.
    Santoshkumar B, Dec 23, 2003
    #4
  5. Santoshkumar B wrote:

    >
    > "Pushkar Pradhan" <> wrote in message
    > news:...
    >> I need to pass a struct by reference to a function:
    >> The struct:
    >> typedef struct tifftags {
    >> uint32 imageWidth, ...;
    >> ...
    >> } TIFFTAGS;
    >> typedef TIFFTAGS * TIFFTAGS_PTR;
    >>
    >> main() {
    >> ....
    >> TIFFTAGS tags;

    >
    > This statement defines a pointer veriable of type TIFFTAGS


    No, it doesn't. It defines a struct object, not a pointer.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Dec 23, 2003
    #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. uny ternally

    Passing a struct as reference

    uny ternally, Sep 7, 2004, in forum: C++
    Replies:
    5
    Views:
    3,044
    uny ternally
    Sep 8, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,356
    Chris Fogelklou
    Apr 20, 2004
  3. Ole
    Replies:
    4
    Views:
    587
    Michael Wojcik
    Oct 26, 2004
  4. beetle
    Replies:
    2
    Views:
    891
    beetle
    Jan 25, 2005
  5. Mike
    Replies:
    12
    Views:
    616
Loading...

Share This Page