Marcel: Dynamisches Struct Array

Beitrag lesen

Hallo

Ich möchte als globales Variable ein Struct Array zur Verfügung stellen.

Daten werden aus einem File eingelesen und im struct abgelegt

  
..  
..  
  
typedef struct {  
   int param1  
   int param2;  
   int param3;  
} stru ;  
  
stru **array;  
  
..  
..  
 // Loops for each line  
 int i = 0;  
 while( fgets(buffer, MAX_LENGTH_LINE + 2, fp) != NULL ) {  
  // Reallocates memory  
  if(  i == REALLOC_AFTER ) {  
   REALLOC_AFTER *= 2;  
   array    = (stru **)realloc(array, REALLOC_AFTER * sizeof(stru *));  
  
   // Checks memory allocation  
   if (array == NULL) {  
    println("Error");  
    exit(EXIT_FAILURE);  
   }  
  }  
  
        chomp ( buffer );  
        char *result = strtok( buffer, SEPARATOR );  
        stru s;  
  
        s.param1 = atoi(result);  
           result = strtok( NULL, SEPARATOR );  
        s.param2 = atoi(result);  
           result = strtok( NULL, SEPARATOR );  
        s.param3 = atoi(result);  
           result = strtok( NULL, SEPARATOR );  
  
        array[i] = &s;  
  
// Ausgabetest OK  
         printf("%i\n", array[i]->param1);  
         printf("%i\n", array[i]->param2);  
         printf("%i\n", array[i]->param3);  
  
         printf("sizeof %i:", sizeof(array));  
  

Greife ich aufs Array aus einer Methode zu, ergibt dies aber ein Fehler, bzw. ich erhalte als Ausgabe irgendwelche grosse Zahlen:

  
void test() {  
         printf("%i\n", array[i]->param1);  
         printf("%i\n", array[i]->param2);  
         printf("%i\n", array[i]->param3);  
  
         printf("sizeof %i:", sizeof(array));  
}  

Kann mir jemand sagen wo das Problem liegt?
Danke.

Gruss M.