0

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];
}
5
  • 1
    Your program invokes undefined behavior. You're accessing your v array out of range. It is only cant items in size, meaning it is only subscriptable by 0..(cant-1). Further v[0].I is indeterminate, yet you rely on its value in the condition of your `for(i-0;i<v[0].I;...) loop.; more undefined behavior. Commented Aug 15, 2016 at 18:04
  • not sure if this is the problem, but you are writing outside the bounds ofr your v array. Indices should be from 0 to cant - 1 Commented Aug 15, 2016 at 18:05
  • 1
    return 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. Commented Aug 15, 2016 at 18:08
  • They are. Look the while, it don't get to 100. Commented Aug 15, 2016 at 18:23
  • it doesn't matter what the while loop is doing. cant is a defined constant. return temp[cant]; is the same as return 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. Commented Aug 15, 2016 at 18:37

1 Answer 1

1

cant is equal to 100, you assign a value to v[cant], that is to v[100], but the array v, with length 100, is assignable only with an index from 0 to 99.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't use index 100. Only on the while below I use the index and don't include the 100. Anyway, If I comment the line v[cant]=completar(); it works fine. So don't why there's a trouble if the function returners temp[cant]. ------------------------------------------------------------------------------------------------------------------- while((temp[i-1].p!=0)&&(temp[i-1].l!=0)&&(i<100)); temp[0].I=i-1; return temp[cant];
@rocbl, the first command of the main is v[cant]=completar();, and cant is a constant with value 100, so the statement is actually assigning a value returned by completar() to v[100]. This is the reason of the segmentation, as you have discovered by commenting that line. The statement return temp[cant]; does not cause the fault since you are only reading v[100], not assigning it, but this means that the statement read a value outside of the vector space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.