[C] Problema durante la stampa di alcune stringhe in linguaggio C
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
#include
int main(void) {
char **arr;
arr = (char **)malloc(4*sizeof(char *));
int i;
for (i=0; i<4; i++)
arr = (char *)malloc(20*sizeof(char));
for (i=0; i<4; i++) {
printf("Insert the name of the alumn\n");
scanf("%s", &arr[1]);
printf("Insert the surname of the alumn\n");
scanf("%s", &arr[2]);
printf("Insert the grade of the alumn\n");
scanf("%s", &arr[3]);
}
for (i=0; i<4; i++) {
printf("The name of the alumn is %s", arr[1]);
printf("The surname of the alumn is %s", arr[2]);
printf("The grade of the alumn is %s", arr[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.
code:
#include
#include
int main(void) {
char **arr;
arr = (char **)malloc(4*sizeof(char *));
int i;
for (i=0; i<4; i++)
arr = (char *)malloc(20*sizeof(char));
for (i=0; i<4; i++) {
printf("Insert the name of the alumn\n");
scanf("%s", &arr[1]);
printf("Insert the surname of the alumn\n");
scanf("%s", &arr[2]);
printf("Insert the grade of the alumn\n");
scanf("%s", &arr[3]);
}
for (i=0; i<4; i++) {
printf("The name of the alumn is %s", arr[1]);
printf("The surname of the alumn is %s", arr[2]);
printf("The grade of the alumn is %s", arr[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.
Risposte
Sarebbe meglio se formattassi il codice inserendolo tra gli opportuni tag, in modo da renderlo più leggibile.
Il problema risiede nel fatto che hai creato una matrice di char non di stringhe.
Hai creato 4 char* e successivamente ognuno di essi lo hai fatto puntare a un array di 20 caratteri (char)
Ma a te servono 4 array di stringhe, non di caratteri... quindi la frase precedente deve diventare:
Hai creato 4 char** e successivamente ognuno di essi lo hai fatto puntare a un array di 20 stringhe (char*)
Il problema risiede nel fatto che hai creato una matrice di char non di stringhe.
char **arr = (char **)malloc(4*sizeof(char *)); for (int i=0; i<4; i++) arr[i] = (char *)malloc(20*sizeof(char));
Hai creato 4 char* e successivamente ognuno di essi lo hai fatto puntare a un array di 20 caratteri (char)
Ma a te servono 4 array di stringhe, non di caratteri... quindi la frase precedente deve diventare:
Hai creato 4 char** e successivamente ognuno di essi lo hai fatto puntare a un array di 20 stringhe (char*)

Scusami per la formattazione, non credevo ci fosse una funzione specifica.
Ho seguito il tuo consiglio ed ora funziona tutto, grazie mille!
Ho seguito il tuo consiglio ed ora funziona tutto, grazie mille!