Adam Bak
Adam Bak

Reputation: 1279

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.

Upvotes: 7

Views: 12018

Answers (5)

sergej
sergej

Reputation: 18009

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.

Upvotes: 11

raja Shekar
raja Shekar

Reputation: 21

The typedef Keyword: There is an easier way to define structs or you could "alias" types you create. For example:

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

Now, you can use Books directly to define variables of Books type without using struct keyword. Following is the example:

Books Book1, Book2;

You can use typedef keyword for non-structs as well as follows:

typedef long int *pint32;

pint32 x, y, z;

x, y and z are all pointers to long ints

Upvotes: 0

ameyCU
ameyCU

Reputation: 16607

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.

No , it's not what you think . It is not because of typedef you can access struct members with synth.

In this example, typedef is not used and yet I am still able to reference this structure through synth.

It is because of synth itself as it is struct variable that you can access members of struct.

typedef is a keyword used in C language to assign alternative names to existing types.Mostly for user-defined data types .

An example -

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

Here now, D can be used to declare struct variable.

Without typedef -

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

synth is a struct variable and you can access members of struct. But in this without typedef if you want to declare struct variable you have to declare like this -

 struct demo t;

In either of case if you even remove typedef then also you can access struct with synth , typedef has no role in that.

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263627

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.

Upvotes: 6

dbush
dbush

Reputation: 225344

This is the same as if you declared:

int i;

And did this:

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

The two lines would print the same value.

If you had this:

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

mydemo synth;

You could do this:

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.

Upvotes: 0

Related Questions