about structure

Discussion in 'C' started by ravindrareddy3, Mar 17, 2010.

  1. ravindrareddy3

    ravindrareddy3 New Member

    Joined:
    Mar 17, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    why we will not create a variable as a datamember in structure?
    for eg:
    struct some
    {
    int a;
    float g;
    struct some n;
    };
     
  2. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    When u create a structure, a definition is being created. But when u insert a variable of the same structure in the definition, the structure is still uninitialised. Hence the compiler cannot recognize the prototype of the variable. and it throws an error.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    A structure is the definition of a new datatype, not the definition of a new variable.
    But you can do both in a single step:
    struct some
    {
    int a;
    float g;
    struct some *n;
    } mysome;

    to define mysome as the above unnamed structure (which means you can't use that structure anywhere else).

    By the way, "struct some n" won't work within the definition of struct some; you have to use pointers, because at that point "struct some" isn't fully defined, so it doesn't know how much space to allocate. But it knows how big a pointer is so that's no problem.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice