1

I have watched videos by different people on youtube and now i am little bit confused

first two[(1) (2)] are of same type without typedef and then two[(3) (4)] are of same type with typedef but the way of writing them is different

Please mention if any of the below represented structure is wrong and why with number. I am assigning number to all of the these formats

(1)

struct tag{
    int number;
    char *name; 
    float num;
};

main()
{
    struct tag name;
    name firstmember;

    ------code-------
    return 0;
}

Next type i have seen is same as the above but :

(2)

struct tag{
    int number;
    char *name; 
    float num;
}name;

main()
{
    name firstmember;

    ------code-------
    return 0;
}

Next is using structure with typedef:

(3)

typedef struct tag{
    int number;
    char *name; 
    float num;
}name;

main()
{
    name firstmember;

    ------code-------
    return 0;
}

same version of typedef with other one:

(4)

struct tag{
    int number;
    char *name; 
    float num;
};

main()
{
    typedef struct tag name;

    name firstmember;

    ------code-------
    return 0;
}

Please mention if any of the the above represented examples have any kind of error

Lastly this doubt is completely different from the above examples

if we don't put a structure tag just after the word struct and do like this

(5)

struct {
    int number;
    char *name; 
    float num;
}tag;

main(){}

does this word 'tag' remains structure tag or if its after '}' so its a structure name now ? What is it? If it becomes the name why do we use the structure tags anyways?

Please respond to this question after reading all my doubts carefully in well written format

dragosht
  • 3,237
  • 2
  • 23
  • 32
  • `tag` is an instance of the structure in #5. I don't know why you're ever using `name` like it's a type in the first two. – chris Jul 21 '14 at 01:06
  • @chris what does that mean 'tag' is an instance of structure. Are rest of the above written structure formats correct? –  Jul 21 '14 at 01:08
  • @chris is this word 'tag' still a structure of now its structure name? that's what i am confused in –  Jul 21 '14 at 01:09
  • It's an object. Just like how `i` in `int i;` is an object. Only #3 works. #4 is missing a `struct` before the `tag` in the `typedef`. – chris Jul 21 '14 at 01:13
  • (5) `tag` means the variable name. It is not possible to create an other object when there is no tag of the structure. – BLUEPIXY Jul 21 '14 at 01:14
  • @chris that was a miss print thanks i understood it now a structure should have tag otherwise we can't create more objects of that type –  Jul 21 '14 at 01:25
  • @Noobsplzdon't-1, Not true. A typedef name will allow you to create any number of objects without a tag name. – chris Jul 21 '14 at 01:35
  • @chris can you tell me what is that word 'tag' in my last (5) example? –  Jul 21 '14 at 01:39
  • @Noobsplzdon't-1, It's a variable. – chris Jul 21 '14 at 01:40
  • is structure tag or structure name? –  Jul 21 '14 at 01:40
  • It's neither a tag name, nor a typedef name. #5 has neither of those. It is impossible to create another instance of that structure after that statement without getting the type from `tag` using something like GCC's `typeof`. – chris Jul 21 '14 at 01:42
  • @chris if i don't use a typedef and that i have to put some tag after sruct to refer to it. Otherwise its not gonna work. And if i use typedef before struct than : typedef struct { members } tag; /////////here 'tag' becomes the structure name? –  Jul 21 '14 at 01:48
  • @Noobsplzdon't-1, It depends on what you mean by working. It will create the variable just fine, but you won't normally be able to make another. If you put the `typedef` there, then `tag` becomes a typedef name for the structure. – chris Jul 21 '14 at 01:52

1 Answers1

0

Have a look at these close threads with popular answers:

Community
  • 1
  • 1
Ruslan Gerasimov
  • 1,752
  • 1
  • 13
  • 20
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – XN16 Jul 21 '14 at 07:46
  • @XN16 I partially agree with your comment, because that's completely right for external links, but if you checked them you could see they all are Stack Overflow. I didn't think those highly voted pages could go away, if so, what's the sense at all? – Ruslan Gerasimov Jul 21 '14 at 08:00