【问题标题】:Array of struct implementation in CC中的结构实现数组
【发布时间】:2014-10-13 07:17:52
【问题描述】:

我需要一些关于我的结构实现数组的想法。这就是我的结构中的内容。我计划使 SHAPES 成为一个数组,因为我将有多个 SHAPES。每个形状将有多个 x 和 y 坐标。因此,我不确定是否应该制作链接列表。开始和结束的目的是我最终将在我的形状正确后运行搜索算法。

struct START
{
    int x; 
    int y;
};
struct END
{
    int x; 
    int y;
};
struct SHAPES
{
    int x [100]; 
    int y [100];
    int *link;
};
struct DISTANCE
{
    int distance_traveled; 
    int distance_to_finish;
};

我正在阅读这篇文章,想知道在创建结构时是否需要 malloc 或 calloc。如果是,为什么,如果不是,为什么? Malloc 和 calloc 总是让我感到困惑。

How do you make an array of structs in C?

int main(int argc, char *argv[])
{

    //Is it better to make these pointers? 
    struct START start;
    struct END end;
    struct SHAPES shapes[100];

    while(fgets(line, 80, fp) != NULL)
    {
        //I have a very annoying issue here, I don't know the size of the lines. 
        //If there are more values I want to increment the x[] and y[] array but NOT the 
        //shapes array. I can only think of one way to deal with this by putting a bunch of 
        //variables in the sscanf. I discovered putting sscanf on the next line didn't do what 
        //I was hoping for. 
        sscanf(line, "%d%*c %d%*c ", &shapes[i].x[i] , &shapes[i].y[i] );
        printf(" line is: %s \n", line);
        sscanf(line, "%d%*c %d%*c ", &x1_main , &y1_main );
        printf(" line is: %s \n", line);
        printf(" shapes[i].x[i] is: %d \n", shapes[i].x[i]);
        printf(" shapes[i].y[i] is: %d \n", shapes[i].y[i]);
        printf(" x1_main is: %d \n", x1_main);
        printf(" y1_main is: %d \n", y1_main);
        i++;
        memset(line, 0, 80);
    }

}

这就是我的文件的样子。添加%*c 似乎可以适当地处理逗号和分号。

10, 4
22, 37
22, 8; 2, 0; 3, 6; 7, 8; 5, 10; 25, 2
1, 2

我从这里得到了这个想法。

https://www.daniweb.com/software-development/c/threads/334515/reading-comma-separated-values-with-fscanf

【问题讨论】:

  • 不清楚为什么 STARTEND 有不同的类型(或者为什么你喜欢大喊大叫;通常,为宏保留所有大写名称,而不是类型 - 标准 @ 987654330@ 和 POSIX DIR * 尽管有)。当然,您可以将此称为“位置”,然后您的Shape 应该包含一个Position 值数组。创建形状的链接列表没有不言而喻的理由。如果您使用 C99 或 C11,您可能会考虑使用灵活的数组成员,但这也可能会让您太难了。
  • 要处理一行中未知数量的条目,请考虑How to use sscanf() in loops?
  • 我强烈怀疑,当每个形状可以容纳 100 个点时,您在数组中使用 100 个形状是错误的。我认为,您应该先填充一个形状的前 100 个点,然后再移动到数组中的另一个条目。
  • 至于malloc() 等:当数组太大而无法在堆栈上分配或者直到运行时才知道它的大小并且任何预分配可能是太小(或需要使用太多空间才合理)。尽可能使用数组;必要时使用列表。数组使您可以轻松访问任何元素,但很难在数组中间删除或添加元素。列表很灵活(很容易添加,也可以从列表中间删除),但与使用数组相比,在列表中查找特定元素通常很慢。
  • @JonathanLeffler 开始和结束的目的是因为我将在正确构建形状后运行搜索算法。我需要 Start 和 End 才能正确运行算法。

标签: c arrays struct


【解决方案1】:

首先,您可能需要考虑这样的事情:

struct point {
      int x;
      int y;
};

因此您可以使用struct point 数据结构(数组)而不是xy 的两个单独的数据结构。像这样使用它还应该加快对点的访问,因为它们的坐标在内存中彼此相邻。否则,x 将在 x 数组的某处,y 在 y 数组的某处。

存储点的数据结构的选择取决于您的使用情况。如果您需要直接寻址点,则链表可能是一个糟糕的选择。如果您总是以线性顺序访问所有点,那很好。但是,考虑到单链表将为next 指针的每个点添加8 个字节。双向链表将为 prev 使用另外 8 个字节(假设是 64 位拱形;通常为 sizeof(pointer))。我假设您创建 x[100]y[100] 以确保您有足够的空间。使用dynamic array(ADT)可能会更好,例如毕竟油嘴滑舌的GArray。无需您做任何事情,它就会增长到您需要的大小。

对于malloccalloc:这并不重要。对calloc 的调用基本上是一个malloc,后跟一个

memset(ptr, 0, sizeof(mallocd area);

即内存归零;参见manpage calloc。如果你直接初始化内存,你可能不需要这样做。

【讨论】:

    【解决方案2】:

    没有指针成员的结构
    如果你的结构没有指针成员,比如:

    typedef struct {
        int a;
        int b;
    
    } DEMO;  
    

    然后您可以像这样简单地声明 typedef 结构的数组实例:

    DEMO demo[10];// instance of array of 10 DEMO
    

    示例,带有指针成员的结构
    如果成员列表中有指针:

    #define SIZE_STR 20
    
    typedef struct {
        int a;
        int b;
        char *str;//pointer, will require memory allocation
    } DEMO;
    
    DEMO demo[10];// instance of array of 10 DEMO
    
    int main(void)
    {
        int i;
    
        for(i=0;i<10;i++)//create memory for each instance of char * in array of DEMO
        {   
            demo[i].str = malloc(SIZE_STR); 
        }
        return 0;
    }  
    

    不要忘记 free() 所有 malloc'ed 内存实例。

    动态分配结构数组
    如果需要为结构动态分配内存:

    typedef struct {
        int a;
        int b;
    } DEMO;  
    DEMO demo, *pDemo;// create a pointer to DEMO
    

    在函数中,main() 例如:

    int main(void)
    {
        pDemo = &demo;  
        pDemo = malloc(sizeof(DEMO)*10);//provides an array of 10 DEMO
        return 0;
    }  
    

    同样,不要忘记 free() 所有 malloc 内存实例。

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多