Program Received Signal SIGSEGV, Segmentation fault

Asked

Viewed 197 times

-1

When I call this function happens this error "Program Received Signal SIGSEGV, Segmentation fault."

void cria_agenda(Agenda *agenda_prof, int Id_prof, char *nome, int ano){
    agenda_prof->quant_compromissos = 0;
    agenda_prof->Id_prof = Id_prof;
    strcpy(agenda_prof->nome_prof, nome);
    agenda_prof->ano = ano;
    
}

The call and that:

Agenda *agenda_prof[P];

int tam_agenda = 0;
char *nome = "felipe";
int ano = 2020;

cria_agenda(agenda_prof[tam_agenda],tam_agenda, nome,ano);

  • I think I should allocate memory to the pointer first

1 answer

0

The problem is that you create an array of Agenda pointers, the correct way to do it would be:

Agenda agenda_prof[P];
cria_agenda(&agenda_prof[tam_agenda],tam_agenda, nome,ano);

The vector you had created only has pointers to schedules, none of them is really an agenda, so it makes no sense to try to access some field that should be on the agenda. Another error that will probably occur in your program is in the section that you use strcpy, I imagine that you are declaring a character pointer field in your scheduler named prof, this will probably generate segmentation fault as well, because there is no space allocated to the pointer and you will try to write on it anyway, remember to use the methods malloc() and/or calloc() not to have this problem.

Note: Next time you post questions here put the complete code so there is no confusion or theorizing about other parts of the code that interfere with your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.