0

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.

8
  • you better check the n value after the fscanf(f_in,"%d",n); because i m kinda sure the problem is there Commented Jan 4, 2019 at 8:23
  • Use brackets after the & in your fscanf statements. Example : &A[i]->locuri should be &(A[i]->locuri) Commented Jan 4, 2019 at 8:24
  • I checked the n and it is ok, it gets the right value. And i also put brackets buts still doesnt work. Acces violating writing location 0xCCCCCCCC Commented Jan 4, 2019 at 8:27
  • It literally works for A[0] but when it goes at A[1] it gives me the error Commented Jan 4, 2019 at 8:30
  • 2
    On a side note, do not use void main() please. Commented Jan 4, 2019 at 8:41

2 Answers 2

3

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 |
                               +--------------------------------------------+
Sign up to request clarification or add additional context in comments.

9 Comments

Ok and now i got stack around A corrupted and stack around n corrupted
When using, pointers-to-pointers-to-ad nauseum, it is very easy to get confused and have an off-by-one error with regards to the dereferencing-depth. Novices like me, often end up, either dereferencing one level too early or one level too deep. Here was a case of one level too early. Not A had n elements, but *A!
Ok so now i can write in the structure members i got stack around variable A corrupted
void aloca(automob *autos, int n) { *autos = (automob)malloc(sizeof(automob)); for (int i = 0; i < n; i++) { autos[i] = (automob*)malloc(sizeof(automob)); } }
Tried to allocate something like this
|
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.