How access to struct in .h , from my principal program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robintux
    New Member
    • May 2010
    • 3

    How access to struct in .h , from my principal program

    i'm writing a header , from define a struct.

    The question is , how declare this struct in my main.c .

    function.c
    Code:
     struct data{
           size_t n;
           gsl_vector  *y;
         };
    function.h
    Code:
    /*here should be the struc data*/
    ?????
    main.c
    Code:
    #include "function.h"
    	struct data d = { n, y 	};
    Errors :

    error: variable `d' has initializer but incomplete type

  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Move what you have posted as being in function.h

    Code:
    struct data{
           size_t n;
           gsl_vector  *y;
         };
    into function.h. Then include function.h in both main.c and function.c

    Do you know about multiple inclusion protection?

    Code:
    /* Nothing here but comments */
    #ifndef FUNCTION_H_INCLUDED
    #define FUNCTION_H_INCLUDED
    
    /* All the declarations in the header file */
    
    
    #enndif /* FUNCTION_H_INCLUDED*/
    /* Nothing here but comments */
    This stops redefinition of type errors if headers get included into source files more than once.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      That should be "Move what you have posted as being in function.c into function.h."

      Comment

      • robintux
        New Member
        • May 2010
        • 3

        #4
        Originally posted by donbock
        That should be "Move what you have posted as being in function.c into function.h."
        right donbock , i read multiple inclusion protection .
        Another way ????

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          "I read multiple inclusion protection. Another way ????"

          I don't understand your question. Please elaborate.

          By the way, the cool pedantic term for "multiple inclusion protection" is idempotency. This word refers to an operation (in this case #include) that produces the same result no matter how many times it is invoked. Casually written header files are not idempotent because C syntax does not allow multiple definitions of the same entity even if those definitions are all the same.

          Comment

          • robintux
            New Member
            • May 2010
            • 3

            #6
            Originally posted by donbock
            "I read multiple inclusion protection. Another way ????"

            I don't understand your question. Please elaborate.
            In the post of banfa

            "Do you know about multiple inclusion protection?"

            I know it's not "multiple inclusion protection"

            Comment

            Working...