Difference between declaration of structs

8

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 as 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 you name the struct ?

    
asked by Maur 11.05.2018 в 13:30
source

3 answers

10

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 called holas based on that structure. By having, in this case, a name the structure, you can 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 follow precisely good practices.

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

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 they will not be able 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, the fact of 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;

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

struct holas unaVariable;
hola otraVariable;
    
answered by 11.05.2018 / 14:10
source
3

The difference between both definitions of structs is the identifier that you include after the reserved word struct . Including the identifier in the declaration of struct allows you to instantiate a variable of that structure, while, if you do not include it, you can only instantiate variables of the same struct only in the definition of struct .

The structs that lack identifiers are anonymous, because they do not have a name that identifies them.

  

In this page of site C Con Clase define the syntax of a structure and an anonymous structure:

struct [<identificador>] { 
    [<tipo> <nombre_objeto>[,<nombre_objeto>,...]];
} [<objeto_estructura>[,<objeto_estructura>,...];
     

The identifier of the structure is an optional name for   refer to the structure.

     

In this other page , you can clarify that the anonymous structures are:

     

There are situations where you can omit both identifiers.

     

An anonymous structure is one that lacks a type identifier   structure and declaration of objects of the type of structure.

     

For example, let's see this statement:

struct stAnonima { 
    struct { 
        int x; 
        int y; 
    }; 
    int z;
};

Here I leave the pages for you to continue reading:

Objects III: Structures

Objects III: Anonymous structures

    
answered by 11.05.2018 в 14:41
1

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 put a name back at the end of the declaration, creating a variable called 'hello', so it is very redundant in my opinion. The best way to do it would be the following:

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

struct hola c = { .edad= "x", .ptr="y" };

Update:

For practical purposes

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

struct hola c = ...

It would be the same as creating a 'hello' type element, saying that the most optimal thing is to create a default data type unalterable, and at the moment of instantiation, give it a name and give it the corresponding values.

    
answered by 11.05.2018 в 13:43