【问题标题】:Defining an array of structures within a structure in C在 C 中的结构中定义结构数组
【发布时间】:2012-10-20 11:04:30
【问题描述】:

您好,我在结构中定义结构数组时遇到了一些麻烦。 这是我的想法,我需要一个名为 figure 的结构,其中包含图形的名称、坐标计数和坐标 (x,y)。每个图形可以有任意数量的坐标。 我还需要能够为不断增加的坐标列表动态重新分配空间......请帮助我指出正确的方向。 谢谢,

泰勒

typedef struct {
  char fig_name[FIGURE_LEN + 1];
  int coordcount;
  /* here i need to declare an array of coord structures that 
     but i am not sure how to do this properly. I was originally
     going to try something like as follows */
  coords *pointer;
  pointer = malloc(sizeof(coords));
  pointer = coords figcoord[];
  /* however i am quite certain that this would not work */
} figure;

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

【问题讨论】:

  • 您不能将默认结构值分配给 C 中的成员变量。您将 代码 隐藏在结构定义的中间。不会在 C 中工作。
  • 必须先声明坐标,然后才能使用它。此外,C 是一种静态类型语言,因此您需要先声明才能进行赋值。
  • @Kira 是的,我认为这是一个问题,但我只是不知道如何解决这个问题。
  • 简单,声明然后赋值,就这样。我相信 WhozGraig 给你的答案说明了我的意思。

标签: c dynamic structure allocation


【解决方案1】:

朝正确的方向踢。尝试这样的事情。对于 malloc() 调用缺少错误检查,我深表歉意,但您会了解大致的想法(我希望):

#include <stdlib.h>

#define FIGURE_LEN  128

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

typedef struct
{
    char fig_name[FIGURE_LEN + 1];
    int coordcount;
    coords *pointer;
} figure;


/* allocate a dynamic allocated figure */
figure* alloc_figure(char* name, int coordcount)
{
    figure *fig = malloc(sizeof(figure));
    fig->coordcount = coordcount;
    fig->pointer = malloc(sizeof(coords) * coordcount);
    strncpy(fig->fig_name, name, FIGURE_LEN);
    fig->fig_name[FIGURE_LEN] = 0;
    return fig;
}

/* release a dynamic allocated figure */
void free_figure(figure** ppfig)
{
    if (!*ppfig)
        return;

    free((*ppfig)->pointer);
    free(*ppfig);
    *ppfig = NULL;
}

int main(int argc, char *argv[])
{
    figure fig;
    fig.coordcount = 10;
    fig.pointer = malloc(10 * sizeof(coords));

    /* access fid.pointer[0..9] here... */
    fig.pointer[0].x = 1.0;
    fig.pointer[0].y = 1.0;

    /* don't  forget to free it when done */
    free(fig.pointer);

    /* dynamic allocation function use */
    figure *fig1 = alloc_figure("fig1", 10);
    figure *fig2 = alloc_figure("fig2", 5);

    fig1->pointer[9].x = 100.00;
    fig2->pointer[0].y = fig1->pointer[9].x;

    /* and use custom free function for releasing them */
    free_figure(&fig1);
    free_figure(&fig2);

    return EXIT_SUCCESS;
}

【讨论】:

    【解决方案2】:

    我认为这样的事情应该可行。

    typedef struct {
      char* figNamePtr;
      int coordCount;
      Coords *cordsPointer;
      //void (*ReleaseMemory)(); // create this function.
    } Figure;
    
    typedef struct {
      double x;
      double y;
    } Coords;
    
    Figure * NewFigure(int coordCount){
        Figure * retVal = (Figure *) malloc(sizeof(Figure));
    
        /* Initialize Figure */
        retVal->figNamePtr = (char *) malloc(sizeof(char) * (FIGURE_LEN + 1));
    
        /* Set the Function pointer, 
           create a function named ReleaseMemory that free up the memory */
        //retVal->ReleaseMemory = ReleaseMemory;
    
        retVal->cordCount = coordCount
    
        retVal->cordsPointer = malloc(sizeof(Coords) * coordCount);
    
        return retVal;
    }
    

    使用此代码,当您想创建一个新图形时,只需使用必要的参数调用函数NewFigure( )。用完之后,叫ReleaseMemory()

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多