Home > Software design >  Does declaring struct Name make Name equivalent to struct Name?
Does declaring struct Name make Name equivalent to struct Name?

Time:05-11

I am a bit confused when using struct in c/c . Traditionally, when I use struct, I usually use it as:

typedef struct Name{
    int a;
};

Name var;

Although it is considered a bad practice from Linus, it's somehow common.

Yet, I wonder

struct Name{
    int a;
};
Name var;

Seems to serve the same purpose, is typedef really necessary? In fact, shouldn't it be

struct Name{
    int a;
};
struct Name var;

Why is the struct from "Name" omitted? If it can always be omitted, does that mean typedef struct is totally useless?

CodePudding user response:

For:

typedef struct Name{
    int a;
};

Name var;

The definition should be:

typedef struct Name{
    int a;
} Name;

Name var;

Otherwise you are not aliasing the type.


In C this doesn't make sense, when you declare struct Name you can already instantiate Name omitting the struct keyword, as you do in the second code snippet, which would not work for C. It behaves similarly to class, in this regard, you don't declare class Name var;, just Name var;.

In C I don't agree it's a bad practice, it's a user defined type, why would you always use struct if you can omit it, any programmer worthy of that name should be able to identify a user defined type, it's not rocket science. This though is opinion based. If it's bad practice we will need to revise the whole notion, not only in C but also C#, where class and struct keywords are always omitted when instantiating objects.

As for aliasing pointers, I completely agree, I do not like it one bit, though this can also be seen as opinion based.

So to answer your direct questions:

Seems to serve the same purpose, is typedef really necessary?

For the purpose you describe in C yes, in C no.

Why is the struct from "Name" omitted?

In C it cannot be ommited, unless you typedefine it, in C not only it can, but it should, it's not needed at all.

If it can always be omitted, does that mean typedef struct is totally useless?

As you can see, it's not useless.

CodePudding user response:

Always using struct keyword while initializing a variable could be lengthy (or whatever you think). That's why there's a typedef keyword (although it have many other uses too).

You can declare a struct with typedef like this:

typedef struct{
    int a;
}var;

or

struct Name{
    int a;
};

typedef struct Name var;

Then use it like:

var my_age;

However, the above explanation covers only C, whereas in C structs are similar to classes but their members are public by default.

So, in C you can directly use the struct's name without typing struct keyword again n again

struct Name {
    int a;
};

Name my_age;

CodePudding user response:

In C and C , rules are different. The code you show is clearly C , it wouldn’t compile in C.

With the following definitions:

struct Foo {
    /*fields*/
};

typedef struct Bar {
    /*fields*/
} Baz;

in C, variable declarations struct Foo var, struct Bar var, Baz var are valid, but plain Foo var or Bar var are not. In C all such variable declarations are valid. Thus, C code would typically look like:

struct Foo {
    /*fields*/
};

typedef struct {
    /*fields*/
} Baz;

struct Foo var1;
Baz var2;

while in C , the typical form is:

struct Foo {
    /*fields*/
};

Foo var;

UPD: technically, that’s because in struct Foo {..., in C type name is struct Foo; while in C the name is simply Foo, and struct is only needed to declare the type, not to use it.

  • Related