0

I've come across this strange way to define a structure in C and I cannot find anywhere what this really is?

    typedef struct {
        int val;
    } *Name;

What is the difference to this?

typedef struct {
   int val;
} Name;

I see however that I have to use "->" on the first and "." on the second, but what is really the difference?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Ludvig W
  • 744
  • 8
  • 27

2 Answers2

4

In the first case

typedef struct {
    int val;
} *Name;

the name Name is declared as an alias for the type pointer to an unnamed structure.

You can use this pointer type for example the following way

Name ptr = malloc( sizeof( *ptr ) );

and then you can write for example

ptr->val = 10;

or

( *ptr ).val = 10;

That is in this declaration there is declared the pointer ptr of the type Name and dynamically created an object of the structure and the pointer points to this dynamically created object.

In the second case

typedef struct {
    int val;
} Name;

the name Name is declared as an alias for the unnamed structure itself.

To declare an object of this type you can just write

Name name;

and then

name.val = 10;

or just

Name name = { 10 };

and an object of the structure type will be created.

Or if you want to allocate an object of the structure type dynamically you can write

Name *ptr = malloc( sizeof( Name ) );

To make it more clear here are another examples of using typedef.

typedef int A[10];

this typedef declares the name A as an alias for the array type int[10].

you can write after that

A a;

This declaration declares array object a of the type A that is equivalent to int[10].

This declaration

typedef int ( *A )[10];

declares the name A as an alias to the type pointer to an array of type int[10].

This declaration can be used for example the following way

int a[10];
A ptr_a = &a;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • When using "*" i have to use malloc to instantiate it, but without that I can simply say Name name; name.val = 3? Why is that? thanks man – Ludvig W Sep 22 '17 at 19:38
  • Thanks, another question if u dont mind, the argument to sizeof is *ptr, does it matter or is it preferable over giving sizeof *Name? – Ludvig W Sep 22 '17 at 19:41
  • @Lurr Name is a type. You may not write *Name. The unary operator * is applied to objects. – Vlad from Moscow Sep 22 '17 at 19:43
4

When you write

typedef struct {
   int val;
} Name;

you're saying "the name Name now refers to a struct with one element, an int named val."

When you write

typedef struct {
   int val;
} *Name;

the star changes the meaning so that this says "the name Name now refers to a pointer to a struct with one member, an int named val." This is why you need to use the arrow operator rather than a dot: in the first case, Name is a type that's an honest-to-goodness structure, and in the second Name is a type that's a pointer to a structure.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065