如何以正确的方式将结构的数组传递给函数?

时间:2019-01-04 08:02:15

标签: c function pointers struct

我试图将结构的数组传递给函数,但是当i变为1,acces violation时给我一个错误。 这是我的代码:

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

我认为指针A分配不正确,但我真的不知道。你有什么想法?因为当我在main函数中编写它时此方法有效,但在read_autos之类的其他函数中对其进行纠正时则无效。

2 个答案:

答案 0 :(得分:4)

A[i] -> locuri的意思是(* A[i]).locuri;如果A是指向automob的指针数组,这将是有道理的;但事实并非如此。您需要(* A)[i].locuri。对于其他字段,依此类推。

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

你写的是什么

+------+     +-------+     +--------------------------------------------+
|  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 |
             |       |     +--------------------------------------------+
             +-------+

您想要什么:

+------+     +-------+         +--------------------------------------------+
|  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 |
                               +--------------------------------------------+

答案 1 :(得分:1)

(这是一条评论,但我没有50的声誉,所以我回答,您可以将其转换为评论)

遇到此类问题并发生时,我会毫不犹豫地简单地打印指针值。
例如,在您的情况下:

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