Hola, a mí me da un error de compilación (el programa no empieza a ejecutarse) y pienso que está relacionado con cómo intentas pasar el struct como parámetro de función.
Aquí pego 3 ejemplos de cómo se puede pasar un struct como parámetro de función en C.
Ejemplo 1. Paso por valor:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
Salida esperada:
Id is: 1
Name is: Raju
Percentage is: 86.500000
Ejemplo 2. Paso de un puntero a la estructura.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0;
}
void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
Salida esperada:
Id is: 1
Name is: Raju
Percentage is: 86.500000
Ejemplo 3. Paso de estructura como variable global.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
Salida esperada:
Id is: 1
Name is: Raju
Percentage is: 86.500000
Salu2