Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct without typedef keyword

I am currently learning about the struct data structure in C and how it is possible to preface this structure with the typedef keyword. This causes the variable names of the actual structure to be placed in different namespaces as explained in several different references:

Difference between 'struct' and 'typedef struct' in C++?

typedef struct vs struct definitions

However, it is unclear as to what is occurring with the example that I am working with:

#include <stdio.h>

struct demo
{
    short low_pass_vcf;
    short filter_coupler;
    short reverb;
    short sequential;
} synth;

int main() 
{    
    printf("Size of struct: %i\n", sizeof(struct demo));
    printf("Size of struct: %i\n", sizeof(synth));
    return 0;
}

In this example, I am able to access the struct data structure through the synth variable name; however, in the examples that I have seen, in order for me to be able to do this the struct needs to be prefaced with typedef. In this example, typedef is not used and yet I am still able to reference this structure through synth. I am wondering what exactly is occurring that C is allowing me to do this? Any explanation would be appreciated. Cheers.

like image 298
Adam Bak Avatar asked Sep 18 '15 15:09

Adam Bak


2 Answers

struct demo
{
    short low_pass_vcf;
    short filter_coupler;
    short reverb;
    short sequential;
} synth;

Is the same as:

// declare struct demo
struct demo
{
    short low_pass_vcf;
    short filter_coupler;
    short reverb;
    short sequential;
};

// define struct variable synth
struct demo synth;

Now you can access the struct members, example:

synth.sequential = 1;

sizeof(struct demo) returns the size of the struct demo.

sizeof(synth) returns the size of the concrete instance synth.

In this case both return the same size.

Update:

Using typedef it could look like this:

// declare new type demo
typedef struct demo
{
    short low_pass_vcf;
    short filter_coupler;
    short reverb;
    short sequential;
} demo_t;

// define demo_t variable synth
demo_t synth;

Update 2:

From the Linux kernel coding style:

Chapter 5: Typedefs

Please don't use things like "vps_t". It's a mistake to use typedef for structures and pointers. When you see a

vps_t a;

in the source, what does it mean? In contrast, if it says

struct virtual_container *a;

you can actually tell what "a" is.

like image 77
sergej Avatar answered Nov 04 '22 18:11

sergej


The typedef keyword is unnecessary for C struct types. The only advantage it gives you is that it creates a one-word name for the type.

The declaration in your example:

struct demo
{
    /* ... */
} synth;

is actually two declarations, one for the type struct demo, and one for an object of that type named synth. It is perhaps more clearly written as two separate declarations:

struct demo
{
    /* ... */
};

struct demo synth;

The first declaration creates a new structure type. Its name is struct demo. The second defines an object of that type, named synth.

The identifier demo by itself, given these declarations, is meaningless; it's a struct tag, and it's meaningful only when preceded by the struct keyword.

synth, on the other hand, is the name of an object (variable). It does not need to be, and in fact cannot be, preceded by the struct keyword.

You can, if you prefer, add a typedef to give the type struct demo a second name. (A typedef does not define a new type; it merely defines a new name, an alias, for an existing type.) For example, a common idiom is:

typedef struct demo {
    /* ... */
} demo;

Like your original declaration, this is really two declarations, a struct definition that creates the type struct demo, and a typedef that creates a new name demo for that type. It could be written as:

struct demo {
    /* ... */
};
typedef struct demo demo;

followed, if you like, by an object declaration:

demo synth;

(Note that there's no need for the struct tag and the typedef name to be distinct, and IMHO it's clearer for them to be the same.)

The choice whether to use typedef for structure types is mostly one of style. My personal preference is not to use typedef for structures; struct demo already has a perfectly good name. But a lot of programmers prefer to have a name that's a single identifier.

like image 23
Keith Thompson Avatar answered Nov 04 '22 19:11

Keith Thompson