how to use the struct in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janes
    New Member
    • Dec 2011
    • 3

    how to use the struct in C

    I create two C files, one is main.c and another is test.c,and then I defined an struct verb in test.c:
    struct image{
    int a;
    char b;
    };
    struct image ss;

    void test() {
    ss.a = 10;
    }
    I want to use the struct verb ss in the main.c,but I failed. Please help me how can I use the struct verb ss in the main.c.
    Thank you very much
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Create a header file name test.h

    then define struct verb in test.h file. include the test.h file in main.c and so on

    Comment

    • janes
      New Member
      • Dec 2011
      • 3

      #3
      Thank you, I think I am a new man, I want you tell me how can I define struct verb in test.h,may is define extern struct image ss in the test.h. That struct image{
      int a;
      char b;
      }; in test.c
      I do this but failed!
      Thank you, Thank you!

      Comment

      • janes
        New Member
        • Dec 2011
        • 3

        #4
        Oh, I solved the problem by johny, I have a little opinion about the using of the keyword struct. I think the struct verb is the new data struct by yourself, that you can't put definition of the struct in the program file, or the compile machine say the struct is not define, so, you can not do this like use int, char or other data struct that define in c compile. That in other C file is already know the int or char data struct, so you can int a in a C file, and define it in a header file. Like this define a global verb: int a int test.c and referencing declaration: extern a in test.h and change it in main.c. Then you use struct verb you can not do like this, that struct s{int a;}; struct s x; in test.c and extern x; in test.h, then change it in main.c--x.a=10;, or the compile machine think it is a error, because it is not know the s struct data struct, you must define the struct s{int a;}; in test.h and int main.c file include the test.h, referencing declaring of it at last, and you will succeed to change it.

        Comment

        Working...