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 one struct
like in the first example,
struct{
int edad;
char *ptr;
}hola;
... or declare it like in the second example?
struct hola{
int edad;
char *ptr;
}holas;
Is the second one clearer when putting a name to the struct
?
Case 1
Here you are declaring a named variable
hola
that is based on an anonymous structure. Note that you won't be able to create any more variables based on this structure since it doesn't have a name.case 2
Here, instead, you create a name structure,
hola
and then declare a named variableholas
based on that structure. By having, in this case, a structure name, you will be able to create new variables based on this structure when you need it:Which option is better? It depends on your needs. In any case, it's not a good idea to happily declare global variables, so your examples don't 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 often advisable to separate the structure declaration from the variable declaration:
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 usingtypedef
it modifies the behavior of the program since it is not going to 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)
In this first case we are creating an anonymous structure and then assigning an alias to that 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 anywhere in the code:
Case 2 (with typedef)
The following example instead:
Declare a named structure
holas
and then assign an alias calledhola
. In this case we will have two different ways to declare variables:The difference between both definitions of
structs
is the identifier that you include after the reserved wordstruct
. Including the identifier in the declaration of astruct
allows a variable of that structure to be instantiated, while if you do not include it, you will only be able to instantiate variables of that structurestruct
only in the definition of thestruct
.Those
structs
without identifiers are anonymous, because they do not have a name that identifies them.Here I leave the pages for you to continue reading:
Objects III: Structures
Objects III: Anonymous Structures
In the first case you are creating an "anonymous" struct and giving it a name at the end of its declaration. In the second you create a named struct, but then you rename it at the end of the declaration, thus creating a variable called 'hello', so it's very redundant in my opinion. The best way to do it would be the following:
Update:
For practical purposes
It would be the same as creating a 'hello' type element, which being said, the most optimal thing is to create an unalterable default data type, and at the moment of instantiating it, give it a name and give it the corresponding values.