本文介绍了如何以正确的方式将结构的数组传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

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

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

你写的是什么

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

这篇关于如何以正确的方式将结构的数组传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 02:17