#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0

    passing a struct to a function driving me crazy


    Hello,

    New guy here. I'm having a hell of a time with an error message I keep on getting in my C compiler (VS). It gives me the error

    "'test' uses undefined struct 'Transaction'"

    test is an object of type Transaction (struct)

    My code looks like this

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #include "mylib.h"
    
    struct Transaction
    {
    int trackingId;
    etc....
    };
    
    int main(void)
    {
    struct Transaction test;
    char strTest[]="abc";
    }
    
    extractor(strTest,test);
    
    return 0;
    }
    It defines a struct, then passes it to extractor(), which is defined in my .h file and implemented in a seperate .c file

    I have tried adding typedef to the struct definition, removing "struct" from the 2nd formal parameter of extractor() and its prototype, placing the struct definition above the function prototype in my .h file, but all to no avail. Here they are:

    Code:
    //mylib.h
    void extractor(char[], struct Transaction); //prototype
    
    
    //mylib.c
    void extractor(char strTest[], struct Transaction test)
    {
    }

    It's been driving me crazy especially since it works ok if I compile everything in a single file. This is really my first program in C and I'd really appreciate any help with it.

    Thanks and tc

    CoreLEx
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I think you need to move the definition of your struct to mylib.h.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    I'm guessing that's cuz your struct is defined in your main file, and mylib.c has no idea what this struct is. Try defining the struct Transaction in mylib.h instead.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Just piping in with, I hope, a helpful hint.

    In a multi-file project, each C/C++ source file is compiled separately from all the others. Each compilation is a separate run of the compiler and nothing whatsoever is remembered between compilations. That means that everything that source file would need to compile has to be available to it.

    That is why a struct declaration that more than one source file would need must be in a header file that those source files can then include.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    Thanks a lot for the replies. That helped a lot!

IMN logo majestic logo threadwatch logo seochat tools logo