#include<stdio.h>
#include<stdlib.h>
typedefstruct
{
char targa[16];
char marca[16];
char modello[16];
int anno_imm;
} automobile;
int main()
{
// Puntatore alla struttura automobile
automobile *a;
int i,n;
printf("Inserire i dati di quante automobili? ");
scanf ("%d",&n);
// Inizializzazione dinamica del vettore di automobili
a = (automobile*) malloc (n*sizeof(automobile));
for (i=0; i<n; i++)
{
printf ("\nAutomobile n.%d\n",i+1);
printf ("Inserisci la targa: ");
scanf ("%s",a->targa);
printf ("Inserisci la marca: ");
scanf ("%s",a->marca);
printf ("Inserisci il modello: ");
scanf ("%s",a->modello);
printf ("Inserisci l'anno di immatricolazione: ");
scanf ("%d",&a->anno_imm);
}
for(i=0; i<n; i++)
{
printf ("Automobile n.%d\n",i+1);
printf("Targa: %d\n",a->targa);
printf("Marca: %d\n",a->marca);
printf("Modello: %d\n",a->modello);
printf("Anno di immatricolazione: %d\n",a->anno_imm);
getchar();
}
free(a);
return 0;
}
Next your compiler should be warning you about several issues. If it isn't you need to see about increasing your compiler warning level.
In function ‘main’:|
main.c|36|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
main.c|37|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
main.c|38|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
These warnings are a good indication as to where to start looking for your problem. You're using the wrong format specifier for the type of variable you supplied. The printf() and scanf() series of functions are very insistant about the specifiers matching the variable types.