Can you define the name of a structure with a variable?

2

The question is as follows (keep in mind that I am just learning C):
Suppose I define a structure similar to this one

 struct Alumnos{  
 char asignatura[25];  
 int nota;  
  }; 

and I want the user to enter a last name, save it in a variable of type char and send it to a function that the only thing it does is create a .. (I do not know what would be called a structure of students?) I referred a:

struct Alumnos -Aca la variable char con el apellido-;  

Suppose .. the surname "ramirez", stored in variable char :

char apellido[]="ramirez";  
struct Alumnos apellido;

and then enter the subject data and note like this:

apellido.nota=4;

Is this possible, and if so, how is it done? (I'm just showing you my idea but it obviously does not work), I was rummaging through other forums and found something like this:

int cont=0;
struct agenda {

    char nom[15];

    int t_f;

    int t_m;

    char dir[30], email[40], grupos[12];

} control[100];

and then within the "add contact" function, what was done was

    fflush(stdin);

    printf("\n%d.Nombre de contacto:", (cont+1));

    gets(control[cont].nom);

but it does not convince me and I do not understand it at all, I also want to access the data of that structure with the last name, so if someone wants to consult it you can do it by entering the last name ... in the end, that is my question, I am grateful Help me. Greetings.

    
asked by Naye's 28.01.2018 в 22:57
source

1 answer

2
  

Suppose I define a structure similar to this one ...

The name of the structure is not the most appropriate ... that structure only stores information about a student ... by calling it Alumnos it is understood that it stores information from several students which is a false assumption. For this case it would be more appropriate to use the singular

struct Alumno {  // ...

And even I would go a little further ... it really is not storing information of a student but actually stores the notes of a student. The most appropriate name would be then:

struct Asignatura{ // ...
  

and I want the user to enter a last name, save it in a variable of type char and send it to a function that only creates a ...

Here I understand that the structure Alumno must also store, at least, the last name of a student ... then what you want is for the function to initialize a structure of type Alumno with the surname that is passed to it as an argument Peeero claro,

struct Asignatura{  
 char asignatura[25];  
 int nota;  
};

struct Alumno
{
  char apellido[50];
  struct Asignatura asignaturas[4]; // <<---
};

Yes, at this point we would have to know if the number of subjects is fixed (I have assumed 5) or it is variable, in which case maybe this would be better:

struct Alumno
{
  char apellido[50];
  int numAsignaturas;
  struct Asignatura *asignaturas;
};

But it is not something that details in the question ... this part is in your hands. For the rest of the answer I will assume that the option chosen is the first one.

The case ... we already have the structures defined ... the function is missing:

struct Alumno NuevoAlumno(char const* apellido)
{
  struct Alumno alumno;

  // Inicializamos todos los bytes a 0
  // Con esto conseguimos que todos los campos de la estructura estén inicializados.
  // No es imprescindible, pero lo prefiero a inicializar las notas con un for
  memset(&alumno,0,sizeof(alumno)); 

  strcpy(alumno.apellido,apellido); // Copiamos el apellido en la estructura

  return alumno;  
}

With this you could do something such that:

struct Alumno alumno = NuevoAlumno("ramirez");
alumno.asignaturas[0].nota = 4;

printf("%s %d\n",alumno.apellido, alumno.asignaturas[0].nota);
    
answered by 29.01.2018 в 08:03