Ho appena finito di studiare le stringhe e le funzioni malloc/calloc in C. Sto provando a mettere insieme le cose ma ho un problema:
code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char **arr;
arr = (char **)malloc(4*sizeof(char *));
int i;
for (i=0; i<4; i++)
arr[i] = (char *)malloc(20*sizeof(char));
for (i=0; i<4; i++) {
printf("Insert the name of the alumn\n");
scanf("%s", &arr[i][1]);
printf("Insert the surname of the alumn\n");
scanf("%s", &arr[i][2]);
printf("Insert the grade of the alumn\n");
scanf("%s", &arr[i][3]);
}
for (i=0; i<4; i++) {
printf("The name of the alumn is %s", arr[i][1]);
printf("The surname of the alumn is %s", arr[i][2]);
printf("The grade of the alumn is %s", arr[i][3]);
}
return 0;
}
Credo che la scanf funzioni a dovere (quantomeno il compilatore non da errore), ma la printf non funziona e per ogni printf nel ciclo alla fine del codice ottengo l'errore:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
Non capisco perché dice che l'argomento 2 ha tipo int. Forse non ho capito bene come sfruttare la malloc in modo da creare un array bidimensionale di stringhe?
Grazie in anticipo per ogni risposta.