Razvan Marian
Razvan Marian

Reputation: 9

How can I pass a structure's array in the right way to my function?

I am trying to pass a structure's array to a function, but it gives me an error when i becomes 1, acces violation. Here is my code:

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

typedef struct {
  int locuri;
  int putere;
  char marca[50];
  char culoare[50];
  int an_fabricatie;
}automob;

void aloca(automob **autos, int n)
{
   *autos = (automob*)malloc(sizeof(automob)*n);
   if (autos == NULL) {
      exit(1);
   }
}

void read_autos(const char* filename, automob **A, int *n)
{
    FILE *f_in = fopen(filename, "r");
    int i = 0, aux;
    if (f_in == NULL) {
        printf("Nu s-a gasit fisierul!");
        _getch();
        exit(0);
    }
    fscanf(f_in, "%d", n);
    aloca(A, *n);
    while (i < (*n)) {
        fscanf(f_in, "%d", &A[i]->locuri);
        fscanf(f_in, "%d", &A[i]->putere);
        fscanf(f_in, "%s", &A[i]->marca);
        fscanf(f_in, "%s", &A[i]->culoare);
        fscanf(f_in, "%d", &A[i]->an_fabricatie);
        i++;
    }
}

void main()
{
    int n;
    automob *A;
    read_autos("autos.in", &A, &n);
    _getch();
}

I think the pointer A is not allocated properly but I really don't know. Do you have any ideas? Because this works when I write it in the main function but does not work if I right it in another function like read_autos.

Upvotes: 0

Views: 93

Answers (2)

Stef1611
Stef1611

Reputation: 2387

(It is a comment but I do not have 50 reputation, so I answer and you could convert it as a comment)

When I have such problem and it happens, I do not hesitate to simply print pointer value.
For example, in your case :

printf("sizeof %I64u\n",sizeof(automob));
printf("Global Addr %I64u\n",*A);
printf("1st elt Addr %I64u\n",&(*A)[0]);
printf("2nd elt Addr %I64u\n",&(*A)[1]);
printf("1st elt / 1st field Addr %I64u\n",&(*A)[0].locuri);
printf("2nd elt / 2nd field Addr %I64u\n",&(*A)[1].locuri);

Upvotes: 1

AlexP
AlexP

Reputation: 4430

A[i] -> locuri means (* A[i]).locuri; this would make sense if A was an array of pointers to automob; but it isn't. You want (* A)[i].locuri. And so on for the other fields.

    fscanf(f_in, "%d", &(* A)[i].locuri);
    fscanf(f_in, "%d", &(* A)[i].putere);
    fscanf(f_in, "%s", (* A)[i].marca);
    fscanf(f_in, "%s", (* A)[i].culoare);
    fscanf(f_in, "%d", &(* A)[i].an_fabricatie);

What you wrote:

+------+     +-------+     +--------------------------------------------+
|  A  -----> | A[0] -----> | locuri | putere | marca | culoare | an_fab |
+------+     |       |     +--------------------------------------------+
             +-------+     +--------------------------------------------+
             | A[1] -----> | locuri | putere | marca | culoare | an_fab |
             |       |     +--------------------------------------------+
             +-------+     +--------------------------------------------+
             | A[2] -----> | locuri | putere | marca | culoare | an_fab |
             |       |     +--------------------------------------------+
             +-------+     +--------------------------------------------+
             | A[3] -----> | locuri | putere | marca | culoare | an_fab |
             |       |     +--------------------------------------------+
             +-------+

What you want:

+------+     +-------+         +--------------------------------------------+
|  A  -----> | * A  -----> [0] | locuri | putere | marca | culoare | an_fab |
+------+     +-------+         +--------------------------------------------+
                           [1] | locuri | putere | marca | culoare | an_fab |
                               +--------------------------------------------+
                           [2] | locuri | putere | marca | culoare | an_fab |
                               +--------------------------------------------+
                           [3] | locuri | putere | marca | culoare | an_fab |
                               +--------------------------------------------+

Upvotes: 3

Related Questions