Problem with Segmention fault

0

I was studying a little, I'm very good at programming, and I came across an error, segmention default , I'll leave my code below, and if possible, someone can figure out why I'm getting this error. p>

NOTE: The error comes after typing the age.

//Leitura de arquivo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  FILE *arq;

  arq = fopen("arq.txt", "a");

  if(arq == NULL) {
    printf("Erro na abertura do arquivo.");
    return 0;
  } else {

  struct fichaAluno {
    char nome[50];
    char disciplina[50];
    int idade;
    float primNota;
    float segNota;
  };

      struct fichaAluno aluno;

      printf("\t====================================================");
      printf("\n\t\t\tCADASTRO DE ALUNO\n");
      printf("\t====================================================");
      printf("\n\n\t\t\tNOME: ");
      fgets(aluno.nome, 50, stdin);
      fprintf(arq, "%s", aluno.nome);

      printf("\n\t\t\tDISCIPLINA: ");
      fgets(aluno.disciplina, 50, stdin);
      fprintf(arq, "%s", aluno.disciplina);

      printf("\n\t\t\tIDADE: ");
      scanf("%d", aluno.idade);
      fprintf(arq, "%d", &aluno.idade);

      printf("\n\t\t\tPRIMEIRA NOTA: ");
      scanf("%f", aluno.primNota);
      fprintf(arq, "%f", &aluno.primNota);

      printf("\n\t\t\tSEGUNDA NOTA: ");
      scanf("%f", &aluno.segNota);
      fprintf(arq, "%f", &aluno.segNota);



      printf("\t====================================================");
      printf("\n\t\t\tVERIFICAR ALUNO\n");
      printf("\t====================================================");
      printf("\n\n\t\t\tNOME: %s", aluno.nome);
      printf("\n\t\t\tDISCIPLINA: %s", aluno.disciplina);
      printf("\n\t\t\tIDADE: %d\n", aluno.idade);
      printf("\n\t\t\tPRIMEIRA NOTA: %.1f\n", aluno.primNota);
      printf("\n\t\t\tSEGUNDA NOTA: %.1f\n", aluno.segNota);

      return 0;

      fclose(arq);
    }
}
    
asked by anonymous 23.04.2017 / 15:52

1 answer

1

Segmentation fault is an error that occurs when you attempt to access (to read or write) an address in RAM that is reserved for another program (or the operating system itself) or does not exist.

In case you are assigning value to variable student.hood in the wrong way. Keep scanf, but do so:

scanf("%d", &aluno.idade);

You should tell the scanf the address of the variable in which you want to save the read value.

Source: link

    
24.04.2017 / 01:22