consider the structure

struct gun
				{
				char name[50];
				int magazinesize;
				float calibre;
				};

i found in the site
http://www.cs.cf.ac.uk/Dave/C/node9.html#SECTION00900000000000000000
that the tag (i.e gun) of the structure is optional....
in that case wouldn't there be a problem when we declare a variable of type struct or if we have more than one structure without tag.....

Recommended Answers

All 2 Replies

Its posible to do that, but in most of the cases you use it, it will fail. For instance, try to compile this and see the results:

#include <stdio.h>

struct
{
    char * name;
    int age;   
};

struct
{
    long artNum;
    float pr;
};

int
main( void )
{
    struct person;
    struct article;
    
    person.name = NULL;
    
    article.pr = 123.32
    
    return 0;
}

I recommend you, not to do this. In fact, its a good programming practice to define new types with typedef, when you want to define a struct. If not, always tag them. Like this,

typedef struct person
{
    char * name;
    int age;   
} personT;

/* or */

struct art
{
    long artNum;
    float pr;
};

Good Luck!

It's best to always tag your structures, even if you typedef them:

typedef struct mystruct {
  /* Members */
} mystruct;

or

struct mystruct {
  /* Members */
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.