I get segmentation fault on v[cant]=completar();
I checked the function completar and it works ok, and the segmentation fault only appears if I do the assignment. So I really don't know what's going on.
#include <stdio.h>
#define cant 100
#define nom 20
struct detalles
{
int l;
char n[nom];
float p;
int I;
};
struct detalles completar();
main()
{
int i;
struct detalles v[cant];
//trouble is here
v[cant]=completar();
for(i=0;i<(v[0].I);i++)
{
printf("\v\v\v\v\v\r");
printf("legajo\t%d\n",v[i].l);
printf("Nombre\t%s\n",v[i].n);
printf("Promedio\t%f\n",v[i].p);
}
return 0;
}
struct detalles completar()
{
int i=0;
struct detalles temp[cant];
printf("Ingrese cero cundo desee terminar\n");
do
{
printf("Ingrese el numero de legajo de un estudiante\t");
scanf("%d",&temp[i].l);
while(temp[i].l<0)
{
printf("El numero ingresado no es valido, intente nuevamente\v\v\r");
printf("Ingrese el numero de legajo de un estudiante\t");
scanf("%d",&temp[i].l);
}
printf("ingrese el nombre del alumno\t");
scanf("%s",temp[i].n);
printf("Ingrese promedio del alumno\t");
scanf("%f",&temp[i].p);
i++;
if(i==100)
{
printf("Se ha superado en numero maximo de estudiantes\n");
}
}
while((temp[i-1].p!=0)&&(temp[i-1].l!=0)&&(i<100));
temp[0].I=i-1;
return temp[cant];
}
varray out of range. It is onlycantitems in size, meaning it is only subscriptable by0..(cant-1). Furtherv[0].Iis indeterminate, yet you rely on its value in the condition of your `for(i-0;i<v[0].I;...) loop.; more undefined behavior.varray. Indices should be from 0 tocant - 1return temp[cant];has the same problem. It doesn't refer to the whole array, it refers to an invalid position one past the end of the array.whileloop is doing.cantis a defined constant.return temp[cant];is the same asreturn temp[100];, which is outside the bounds of the array. If you want to return the last element in the array,return temp[cant-1];instead.