0

好的,伙计们,请不要对我刻薄,我只是一个女孩,我正在尝试这个编码的东西,并且被它弄得非常困惑。

我有这个练习来创建一个抽象数据类型,其中一部分我需要从文件中获取一些值并使用它们创建一行。在文件上,我首先有行中的点数,然后是每个点的对坐标。我正在使用的结构是这样的:

    typedef struct ponto{
      double x;
      double y;
       } ponto;

    typedef struct linha{
      double numVertices;
      ponto verticesLin[ ];
       }linha;

我需要使用的功能是:

void criaLinha (linha *, double , ponto *);

所以我写了这段代码,通过使用该函数将数据从文件转换到buff,然后到struct linha:

    BuffToLine(ponto buff[], numMax, linha *l){
      double i;
      ponto Aux;
       for(i=0, i<numMax , i++){
           Aux.x = buff[i].x;
           Aux.y = buff[i].y;
        criaLinha(*l, i, *Aux);
                       }
                      }

    void criaLinha (linha *l, double numVertices, ponto *vertices){

       *l.verticesLin[numVertices].x = Aux.x;
       *l.verticesLin[numVertices].y = Aux.y;
                 }

问题是,我不知道如何将值从文件传递到缓冲区,而且我不确定我编写的代码是否有效,因为我无法测试没有缓冲区。那么...有人可以帮助我了解如何创建此缓冲区,以及是否有更好的方法来创建具有“criaLinha”功能的线条?

4

1 回答 1

0

你的代码有很多问题。

第一的:

ponto verticesLin[ ];

不是有效的标准 C,gcc将接受这一点,但如果您希望在结构中有一个尾数组,那么正确的声明是:

ponto verticesLin[0];

如果你这样做,那么你需要为结构分配足够的内存。

第二:

 double i;

不要将doubletype 用于数组索引,使用intor size_t

第三:正确缩进代码并给变量起有意义的名称是一种很好的做法,否则即使您下个月也无法阅读它。

现在,如果您将结构声明为

typedef struct ponto {
    double x;
    double y;
} ponto;

typedef struct linha {
    size_t numVertices; // counters should have an integer type
    ponto verticesLin[0];
} linha;

那么你需要在创建实例时分配足够的空间linha

linha * l = malloc(sizeof(l) + numVertices * sizeof(ponto));

当你传递你的参数时,你还需要让你的类型正确:

void criaLinha (linha * l, size_t numVertices, ponto * vertices);

BuffToLine(ponto buff[], size_t numMax, linha * l) {
    size_t i; // should be an integer type
    ponto Aux;
    for(i=0, i< numMax , i++){
        Aux = buff[i]; // you can copy the whole thing, no need to to it by variable
        criaLinha(
            l // l is already a pointer no need to dereference it
            , i
            , &Aux // Aux is a ponto, you need a ponto*, so you need to take the address of it
        );
    }
}

希望这会有所帮助,您没有提供足够的代码来帮助您获得更多帮助。

于 2013-09-01T17:02:30.463 回答