Struct without the keyword typedef

I am currently studying the struct data structure in C and how to predicate this structure using the typedef keyword. This leads to the fact that the names of the variables of the actual structure are located in different namespaces, as described in several different links:

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

typedef struct vs structure definitions

However, it is unclear what happens to the example I'm 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 can access the struct data structure through the variable name synth ; however, in the examples I saw, in order for me to be able to do this, the struct must be prefaced with a typedef . In this example, typedef not used, and yet I can still reference this structure through synth . I am wondering what exactly is happening, what C allows me to do this? Any explanation would be appreciated. Greetings.

+5
source share
5 answers
 struct demo { short low_pass_vcf; short filter_coupler; short reverb; short sequential; } synth; 

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 structure elements, for example:

 synth.sequential = 1; 

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

sizeof(synth) returns the size of a specific synth instance.

In this case, both return the same size.

Update:

Using typedef , it might 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 Linux Coding Style :

Chapter 5: Typedefs

Please do not use things like " vps_t ". It is a mistake to use typedef for structures and pointers. When you see

 vps_t a; 

in the source, what does it mean? On the contrary, if he speaks

 struct virtual_container *a; 

you can really say what an "a" is.

+6
source

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

The declaration in your example:

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

there are actually two declarations, one for a struct demo type and one for an object of this type called synth . It may be more clearly written as two separate declarations:

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

The first declaration creates a new type of structure. His name is struct demo . The second defines an object of this type called synth .

The demo identifier alone, given these declarations, makes no sense; it is a structure tag, and it only makes sense when the struct keyword precedes.

synth , on the other hand, is the name of an object (variable). It should not be, and indeed cannot be, preceded by the struct keyword.

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

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

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

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

follows, if you want, by declaring the object:

 demo synth; 

(Note: there is no need for the struct tag and typedef to be different, and IMHO it is clearer for them to be the same.)

Choosing whether to use typedef for structure types is basically one of the styles. My personal preference is not to use typedef for structures; struct demo already has a great name. But many programmers prefer to have a name that has one identifier.

+2
source

This is the same as you declared:

 int i; 

And did the following:

 printf("Size of int: %i\n", sizeof(int)); printf("Size of int: %i\n", sizeof(i)); 

Two lines will print the same value.

If you have this:

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

You can do it:

 printf("Size of struct: %i\n", sizeof(struct demo)); printf("Size of struct: %i\n", sizeof(mydemo)); printf("Size of struct: %i\n", sizeof(synth)); 

And they would also print the same thing.

0
source

I can access the struct data structure through the variable name synth ; however, in the examples I saw, in order for me to be able to do this, the struct must be pre-marked with typedef .

No, that’s not what you think. Not because of typedef you can access structure elements with synth .

In this example, typedef not used, and yet I can still reference this structure through synth .

It is because of synth , since it is a struct variable , you can access members of the structure.

typedef is a C keyword used to assign alternative names to existing types. Most of all for custom data types.

Example -

 typedef struct A{ char B; int C; }D ; 

Here D can now be used to declare a structural variable.

Without typedef -

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

synth is a structural variable, and you can refer to members of the structure. But in this case, without typedef , if you want to declare a structural variable, you must declare like this:

  struct demo t; 

In any case, even if you delete typedef , you can also access struct with synth , typedef has no role in this.

0
source

Typedef keyword: There is an easier way to define structures, or you can create alias types. For instance:

 typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; }Books; 

Now you can directly use books to define variables of type Books without using the struct keyword. The following is an example:

 Books Book1, Book2; 

You can use the typedef keyword for nonstructures, as well as the following:

 typedef long int *pint32; pint32 x, y, z; 

x, y and z are all pointers to long ints

0
source

All Articles