This code is about 'struct' in C..
I created a struct spieler with the properties name and age.. By using the for-loop I let the user to create the struct objects. they are named as sp[i] --> sp1, sp2 etc.
the problem is the objects are created. But I can use them only inside the for-loop. If I want to get the value of "sp1.name" in main function, it doesn't work. How can I solve it?
struct spieler{
char name[20];
int age;
};
void erzeuge();
int main() {
int anzahl = 2;
printf("Anzahl Spielern: ");
scanf("%d",&anzahl);
erzeuge(anzahl);
printf("Es sind %d Spielern",anzahl);
/*for(i;i<anzahl;i++){
printf("%d.%s",i, sp[i].name);
}*/
getchar();
}
void erzeuge(int anzahl){
int i=0;
for(i;i<anzahl;i++){
struct spieler sp[i];
printf("Struct fuer Spieler_%d wurde erzeugt\n", i);
getchar();
printf("Name: ");
scanf("%s",sp[i].name);
printf("%s\n",sp[i].name);
}
struct spieler sp[i]- it looks like nonsense to me. Does it even compile? 8-Ostruct spieler sp[i];declares an array ofstruct spielers that isielements long [0,i-1], andsp[i].nameaccesses elementiof it which is an error.istruct spielers. It compiles, but the array vanishes at the next iteration of the loop.