Difference between declaration of structs

Question:

1st code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct{
   int edad;
   char *ptr;

}hola;

int main(){

    hola.edad=2;

    printf("%d\n",hola.edad);
    hola.ptr=malloc(5);
    strcpy(hola.ptr,"Hola");

    printf("%s",hola.ptr);
    free(hola.ptr);


    return 0;
}

2nd code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct hola{
   int edad;
   char *ptr;

}holas;

int main(){

    holas.edad=2;

    printf("%d\n",holas.edad);
    holas.ptr=malloc(5);
    strcpy(holas.ptr,"Hola");

    printf("%s",holas.ptr);
    free(holas.ptr);

    return 0;
}

What difference would it make to declare a struct like in the first example,

struct{
   int edad;
   char *ptr;

}hola;

… or declare it as in the second example?

struct hola{
   int edad;
   char *ptr;

}holas;

Is the second one clearer when giving the struct a name?

Answer:

Case 1

struct{
   int edad;
   char *ptr;

}hola;

Here you are declaring a variable called hola that is based on an anonymous structure. Note that you will not be able to create more variables based on this structure since it has no name.

Case 2

struct hola{
   int edad;
   char *ptr;

}holas;

Here, instead, you create a structure named hola and then declare a variable named holas based on that structure. By having, in this case, a name for the structure, you will be able to create new variables based on this structure when you need it:

struct hola otraVariable;

Which option is better? It depends on your needs. In any case it is not a good idea to go around declaring global variables with joy so your examples do not exactly follow good practices.

On the other hand, declaring the variable at the same time as the structure can lead to less readable code. My experience is that it is usually advisable to separate the structure declaration from the variable declaration:

struct hola
{
  /* ... */
};

struct hola unaVariable;

To make the declaration of variables based on structures less cumbersome, we can resort to the use of typedef . As you can see in the following two examples, the simple fact of using typedef modifies the behavior of the program since it will not be possible to declare structures and variables at the same time, so this is another point in favor to avoid confusion.

Case 1 (with typedef)

typedef struct{
   int edad;
   char *ptr;
}hola;

In this first case we are creating an anonymous structure and then we assign an alias to said structure (Note that variables are no longer declared here). Despite being an anonymous structure, having an alias will allow us to declare variables based on this structure in any part of the code:

hola unaVariable;

Case 2 (with typedef)

The following example, instead:

typedef struct holas{
   int edad;
   char *ptr;
}hola;

Declare a structure named holas and then assign it an alias called hola . In this case we will have two different ways to declare variables:

struct holas unaVariable;
hola otraVariable;
Scroll to Top