gpt4 book ai didi

c - 结构分配的段错误 C

转载 作者:行者123 更新时间:2023-11-30 15:06:30 25 4
gpt4 key购买 nike

我在 v[cant]=completar(); 上遇到段错误我检查了函数 completar,它工作正常,并且仅当我执行分配时才会出现段错误。所以我真的不知道发生了什么。

#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];
}

最佳答案

cant等于100,你给v[cant]赋值,即v[100],但是数组 v 的长度为 100,只能使用从 099 的索引进行分配。

关于c - 结构分配的段错误 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com