Matrix in C language, error zsh: abort

Asked

Viewed 46 times

0

I’m trying to create a matrix to read the names of 5 students.

#include <stdio.h>
#include <stdlib.h>
    
int main(void){
    //Programa para calcular a média das notas de 5 alunos, sendo que cada aluno possui 3 notas.
   char nomes[5][30];
   float notas[5][4];
   float media[5];
   int cont1, cont2;
    
       
   //Leitura dos nomes e das notas de cada aluno.
   for(cont1 = 0;cont1 < 5; cont1++){
      scanf("%s", &nomes[cont1][30]);
   }
    
   //Imprimindo os nomes para testar
   for(cont2 = 0;cont2 < 5; cont2++){
      setbuf(stdin, 0);
      printf("\n%s", nomes[cont2]);
   }
    
   return(0);
}

The code is working to read the 5 names, however it displays only 3 names and presents the error zsh: abort. What might be going on?

1 answer

1

The problem is that the names are not being read correctly. To read a string through the scanf you need to pass the address of the first character of the string. See a corrected example:

//Leitura dos nomes e das notas de cada aluno.
for(cont1 = 0; cont1 < 5; cont1++){
    scanf("%s", &nomes[cont1][0]);
}

Another even simpler example:

//Leitura dos nomes e das notas de cada aluno.
for(cont1 = 0; cont1 < 5; cont1++){
   scanf("%s", nomes[cont1]);
}
  • In this case, what I described is being considered a vector or a matrix?

  • @user196549 char nomes[5][30] is a vector of vectors of char, or is a matrix of char. Basically matrices are vectors that store vectors.

Browser other questions tagged

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