Thread: The difference between declaration and definition of structs. Need explanation please

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    9

    Question The difference between declaration and definition of structs. Need explanation please

    Hello,

    Lastly I started to work with structs (still learning this topic).
    Mainly, my knowledge sources are based upon three things:

    1. "C How to Program" by Deitel.

    2. "Advanced C" by Hipson.

    3. Google ))))

    While reading Hipson's chapter on structs, I've found some part that I can't entirely understand.
    Suppose we're talking about general repr. of a struct:
    Code:
    struct tag_name{
    
        type member_name;
    
        type member_name;
    
        type member_name;
    
    }structure_name;
    So the book says:
    1. If a structure_name is specified and a tag_name is not specified, the structure is being declared but not defined.

    2. If a structure_name is not specified and a tag_name is specified, the structure is being defined but not declared.

    3. If a structure_name and a tag_name are provided, the structure is being both defined and declared.

    4. If neither a structure_name nor a tag_name is provided, a compile-time error will result.
    Now assume I have a following code (just to get some example):
    Code:
    #include<stdio.h>
    
    int main(void){
        
    // case 1
    
        struct {
            int x;
            int y;
        }my_test;
    
    
        my_test.x = 4;
        my_test.y = 5;
    
        //despite being "not defined", I get an output.
    
        printf("x = %d, y = %d\n", my_test.x, my_test.y);
    
        //case 2
         
        // I define struct here:
    
        struct my_test_2{
            int a;
            int b;
        };
    
        // and declare it here, by creating an instance of it named "my_var"?
        
        struct my_test_2 my_var;
    
    
        my_var.a = 15;
        my_var.b = 37;
    
    
        printf("a = %d, b = %d\n", my_var.a, my_var.b);
    
    
        return 0;
    }
    I have a problem with case # 1, where my structure is declared but not defined.

    When talking about functions definitions and declarations it looks pretty clear to me (hope this analogy is applicable to structs):

    1. function prototype (declaration) something like:
    Code:
    int foo(int, int);
    2.
    Code:
    main(void){. . . }
    3. function definition:
    Code:
    int foo(int num_1, int num_2){. . . }
    But what does it mean: struct that being declared but not defined?

    The same question goes when "typedef" is used:
    In case I write this:
    Code:
    typedef struct{
    int x;
    int y;
    }my_test;
    I know that typedef creates just alias names for struct this alias name is "my_test", and then in order to create an instance of it I must do the following:

    Code:
    my_test my_var_1;
    
    //and only after, write the these assignment statements:
    
    my_var_1.x = 4;
    my_var_2.y = 5;
    But what about struct itself, is been declared or defined such way?

    Finally what is going here:
    Code:
    typedef struct my_test{
    int x;
    int y;
    }my_alias; 
    
    //(is it declared/defined or both?)
    I can write it in two ways:

    1. my_alias my_num; (declaring variable of type struct my_test)

    OR doing the same thing direct way:

    2. struct my_test my_num;


    Sorry for being verbose, just wanted to describe the problem.

    Thanks in advance for help.

    P.S. For those asking:" why didn't you use google?" The answer is simple: while googling I've found couple of sites where a explanations were exactly opposite. So I don't know where "the truth is".

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am cautious here because I tend to ascribe an understanding of declaration versus definition here that comes from C++ classes. Under this borrowed understanding, this is a definition of a struct type with tag name X:
    Code:
    struct X
    {
        int n;
    };
    The above definition is also a declaration. Now, this is a declaration of the same struct type that is not a definition:
    Code:
    struct X;
    On the other hand, this is a definition of an object of the struct type:
    Code:
    struct X x_object;
    The above definition is also a declaration. Now, yhis is a declaration of the object that is not a definition:
    Code:
    extern struct X x_object;
    This is a definition of the object immediately following the definition of the struct type:
    Code:
    struct X
    {
        int n;
    } x_object;
    Now, this is a definition of an object of an anonymous struct type:
    Code:
    struct
    {
        int n;
    } x_object;
    These terms are easy to understand in that they conform to a variant of the "one definition rule", i.e., you can declare the struct type with tag name X many times in the same translation unit, but you can only define it once in a given translation unit. Similiarly, you can declare the same (global) variable many times in various translation units, but you must define it in exactly one translation unit.

    However, in the language of the C standard, this is a declaration of a structure with tag name X such that it specifies a type:
    Code:
    struct X
    {
        int n;
    };
    But this specifies a structure and declares the identifier as a tag of that type:
    Code:
    struct X;
    That is, it is a declaration of the tag as an identifier rather than a declaration of the structure. I may be mistaken, but I do not think the C standard talks about structure definitions. If I am right, then Hipson is mistaken. Furthermore, it seems to be that Hipson does not clearly differentiate between the struct type and the object of the struct type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's the difference between declaring and defining in C and C++ - Cprogramming.com

    I think a declaration just tells you what something looks like - say a function prototype, a struct, a class, or an extern variable.

    A definition causes some amount of memory to be used - whether it's an actual function or some instance of a variable or a class/struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    9
    To: laserlight

    OMG thanks for the answer. I must admit that this is one of those rare cases which made me to execute additional googling ^_^. What a wonderful Pandora box I've opened here
    Yep, it took me some time to read and actually try to understand your answer.

    Still, if you don't I would like to clarify some nuances.

    On the other hand, this is a definition of an object of the struct type:
    Code:
    struct X x_object;
    So in this case I have an object that is fully defined. But in case where previous line of code was:
    Code:
    struct X;
    My struct is not defined at all. Am I right?

    The second question is about this "anonymous" struct type.
    Now, this is a definition of an object of an anonymous struct type:
    Code:
    struct
    {
        int n;
    } x_object;
    It's like to create a struct looking object ... so... may it "hurt" somewhere? I mean, while using this anonymous struct I'm still able to read/write or to determine its length or even pass to a function. Where is a pitfall then?

    Again, thanks for your answer.


    To: Salem

    A definition causes some amount of memory to be used
    I'm not sure that this is what's going on, with structs. Every place that I've googled and every book I've read on this topic say that that there is no memory allocation occurs. Still I have only "2 min" of experience with C, so I may be mistaken.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bravoelf
    My struct is not defined at all. Am I right?
    Yes, it is not, at least using terminology borrowed from C++.

    Quote Originally Posted by bravoelf
    It's like to create a struct looking object ... so... may it "hurt" somewhere? I mean, while using this anonymous struct I'm still able to read/write or to determine its length or even pass to a function. Where is a pitfall then?
    You cannot create any other objects of this struct type. However, this could be used in conjunction with a typedef, in which case you could create other objects of the struct type.

    Quote Originally Posted by bravoelf
    I'm not sure that this is what's going on, with structs. Every place that I've googled and every book I've read on this topic say that that there is no memory allocation occurs.
    Hence the language employed by the C standard does not use "definition" with respect to struct types as far as I know.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-11-2012, 12:27 PM
  2. Difference between definition and declaration
    By Allen in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2012, 03:53 AM
  3. Difference between Declaration , initialisation and definition.
    By nkrao123@gmail. in forum C Programming
    Replies: 12
    Last Post: 09-11-2011, 10:07 AM
  4. declaration and definition
    By sugarfree in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2010, 06:53 AM
  5. Is int i; is a declaration or definition?
    By forumuser in forum C Programming
    Replies: 11
    Last Post: 05-30-2007, 04:43 AM

Tags for this Thread