1

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);
}

5
  • You can't do anything like this. Commented Dec 9, 2011 at 23:54
  • You need a global variable/array to access it in main and other functions. Here you create just a local variable. And I personally don't understand what actually does line struct spieler sp[i] - it looks like nonsense to me. Does it even compile? 8-O Commented Dec 9, 2011 at 23:56
  • 2
    struct spieler sp[i]; declares an array of struct spielers that is i elements long [0,i-1], and sp[i].name accesses element i of it which is an error. Commented Dec 9, 2011 at 23:58
  • 1
    @AlKepp In C99, it declares a local variable length array of i struct spielers. It compiles, but the array vanishes at the next iteration of the loop. Commented Dec 9, 2011 at 23:59
  • Thanks, Daniel, +1. So it's pretty local array. And it is too short. dmckee +1 Commented Dec 10, 2011 at 3:09

3 Answers 3

1

You should declare sp as a pointer at the global scope, and allocate memory for it inside the function erzeuge using malloc.

#include <stdlib.h>
#include <stdio.h>

struct spieler {
    char name[20];
    int age;
};

struct spieler *sp;    // Add this

void erzeuge();

int main() {
    int anzahl;
    printf("Anzahl Spielern: ");
    scanf("%d", &anzahl);

    erzeuge(anzahl);

    printf("Es sind %d Spielern\n", anzahl);

    int i;
    for(i = 0; i < anzahl; i++){
        printf("%d.%s\n", i, sp[i].name);
    }

    if (sp) {
        free(sp);
    }
    getchar();
    return 0;
}

void erzeuge(int anzahl) {
    // Add the following line to allocate memory
    sp = (struct spieler*) malloc(anzahl * sizeof(struct spieler));

    int i;
    for (i = 0; i < anzahl; i++) {
        // Remove the following line because it create an array of "i" elements
        // 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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot. It works. <br> but what do you mean by "// Remove the following line because it create an array of "i" elements "?
You used that line to declare the i-th item of the sp array, doesn't it? That's the wrong way. That line will declare a local array (inside the for loop) named sp with total i elements, which means the indexes range from 0 to i-1. By using that line, neither you can access sp from outside of the loop and from main() function nor you can safely access sp[i].
1

You'd have to return an array of players from erzeuge, something like

struct spieler *erzeuge(int anzahl){
    struct spieler *mannschaft = malloc(anzahl*sizeof(struct spieler));
    int i;
    for(i = 0; i < anzahl; ++i){
        // prompt
        scanf("%18s",&mannschaft[i].name);
        ...
    }
    return mannschaft;
}

Comments

0

Alternative solution without malloc:

void erzeuge(struct spieler* sp, int anzahl)
{
    ...
}

int main()
{
    int anzahl = 2;

    ...

    struct spieler sp[anzahl];
    erzeuge(sp,anzahl);

    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.