结构处理和结构数组

时间:2016-03-09 09:38:55

标签: c arrays pointers struct

我正在编写一个代码来存储文本文件中的数据。代码编译但似乎停止在ReadPanelBlock函数的for循环中运行。它的结构如下: CHBStructure包含CHBPanels,其中包含CHBOpenings。创建这些函数是为了从输入文本文件中读取,我将其组织成数据块以便于阅读。

typedef struct _open
{
    int id;
    double length;
    double height;
    double origX;
    double origY;
    int frames;

    double thickness;
    double E;
    double v;
}CHBOpening;

typedef struct _panels
{
    int id;
    double length;
    double height;
    double origX;
    double origY;
    double origZ;
    double angle;
    int nOpenings;
    int nReinforcement;
    double *xReinf;
    double sx;
    double xReinf0;

    CHBUnit *chb;
    CHBOpening *openings[];
}CHBPanel;

typedef struct _chb
{
    int nStories;
    int nModes;
    int nIter;
    int nPanels;
    CHBPanel *panels[];
}CHBStructure;

int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
    CHBOpening *openings = malloc(sizeof(CHBOpening));
    *S->panels = malloc(S->nPanels*sizeof(CHBPanel));
    for (i=0; i<S->nPanels; i++)
    {
        fscanf(fp,"%d",&S->panels[i]->id);
        fscanf(fp,"%lf",&S->panels[i]->length);
        fscanf(fp,"%lf",&S->panels[i]->height);
        fscanf(fp,"%lf",&S->panels[i]->angle);
        fscanf(fp,"%lf",&S->panels[i]->origX);
        fscanf(fp,"%lf",&S->panels[i]->origY);
        fscanf(fp,"%lf",&S->panels[i]->origZ);
        fscanf(fp,"%d",&S->panels[i]->nOpenings);
        if (S->panels[i]->nOpenings > 0)
        {
            for (j=0; j<S->panels[i]->nOpenings;j++)
            {
                openings = S->panels[i]->openings[j];
                fscanf(fp,"%d",&openings->id);
                fscanf(fp,"%lf",&openings->length);
                fscanf(fp,"%lf",&openings->height);
                fscanf(fp,"%lf",&openings->origX);
                fscanf(fp,"%lf",&openings->origY);
            }
        }
    }
    return 1;
}

编辑1:我尝试通过在每行中打印文本来跟踪发生错误的位置,并且它似乎在ReadPanelBlock函数中的第一个fscanf函数中崩溃。

1 个答案:

答案 0 :(得分:2)

访问S->panels[i]->openings[j]Output running,因为您没有为该数组分配空间。

    if (S->panels[i].nOpenings > 0)
    {
         S->panels[i].openings = malloc(sizeof(CHBPanel)*S->panels[i].nOpenings);

         // inner for
    }

预先为openings指针分配的空间:

CHBOpening *openings = malloc(sizeof(CHBOpening));

会导致内存泄漏,因为j内部for代码

导致地址丢失,因此没有代码可以释放内存
openings = S->panels[i]->openings[j];

您可以避免使用指针并写入

        for (j=0; j<S->panels[i].nOpenings;j++)
        {
            fscanf(fp,"%d",&S->panels[i].openings[j].id);
            fscanf(fp,"%lf",&S->panels[i].openings[j].length);
            fscanf(fp,"%lf",&S->panels[i].openings[j].height);
            fscanf(fp,"%lf",&S->panels[i].openings[j].origX);
            fscanf(fp,"%lf",&S->panels[i].openings[j].origY);
        }

在您的结构中CHBPanel *panels[];CHBOpening *openings[];必须是:

CHBPanel *panels; 
CHBOpening *openings;

最后但并非最不重要的事情:您必须学习一些关于指针以及->.用法的内容。

您的代码应该是:

int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
    int i,j;
    S->panels = malloc(S->nPanels*sizeof(CHBPanel));
    for (i=0; i< S->nPanels; i++)
    {
        fscanf(fp,"%d",&S->panels[i].id);
        fscanf(fp,"%lf",&S->panels[i].length);
        fscanf(fp,"%lf",&S->panels[i].height);
        fscanf(fp,"%lf",&S->panels[i].angle);
        fscanf(fp,"%lf",&S->panels[i].origX);
        fscanf(fp,"%lf",&S->panels[i].origY);
        fscanf(fp,"%lf",&S->panels[i].origZ);
        fscanf(fp,"%d",&S->panels[i].nOpenings);
        if (S->panels[i].nOpenings > 0)
        {
            S->panels[i].openings = malloc(sizeof(CHBOpening)*S->panels[i].nOpenings);

            for (j=0; j<S->panels[i].nOpenings;j++)
            {
                fscanf(fp,"%d",&S->panels[i].openings[j].id);
                fscanf(fp,"%lf",&S->panels[i].openings[j].length);
                fscanf(fp,"%lf",&S->panels[i].openings[j].height);
                fscanf(fp,"%lf",&S->panels[i].openings[j].origX);
                fscanf(fp,"%lf",&S->panels[i].openings[j].origY);
            }
        }
    }
    return 1;
}