【问题标题】:Array of structs - deleting/adding elements and printing结构数组 - 删除/添加元素和打印
【发布时间】:2012-08-17 11:00:42
【问题描述】:

下面的代码

struct orderSlip
{
    char source[64];
    char destination[64];
    char item[64];  
    int position;
};
//global
struct orderSlip data[100];

除了以下这些方法之外,还有其他方法可以打印出每个元素的数据:

printf("%s\n",data[0].source);
    printf("%s\n",data[0].destination);
    printf("%s\n",data[0].item);
    printf("%i\n\n", data[0].position);

    printf("%s\n",data[1].source);
    printf("%s\n",data[1].destination);
    printf("%s\n",data[1].item);
    printf("%i\n", data[1].position);

for(int n = 0; n< 3; n++)
{
    printf("%s\n",data[n].source);
    printf("%s\n",data[n].destination);
    printf("%s\n",data[n].item);
    printf("%i\n\n", data[n].position);
}

对于删除和添加,我是否必须创建一个动态结构数组?如果是这样,那么最简单的语法是什么? 像这样的 c++ 代码

int * bobby;
bobby = new int [5];
delete bobby[5];

但是在 C 中呢?我猜它与malloc和free有关

【问题讨论】:

  • 那是delete[] bobby;,但是是的,您需要malloc/free 在C 中执行此操作。我相信您可以通过一些搜索了解如何在C 中使用动态数组。

标签: c arrays struct printf


【解决方案1】:

"对于删除和添加,我是否必须创建一个动态的结构数组?如果是,那么最简单的语法是什么?像这样的 c++ 代码"

如果您知道您永远不会拥有超过 x 数量的物品,或者至少检查以确保您没有超过您计划的最大数量,则不会。然后你就可以使用你的静态数组了。

添加只需要你有一个变量来跟踪数组中有多少项:

void add_item(struct orderSlip *p,struct orderSlip a,int * num_items)
{
   if ( *num_items < MAX_ITEMS )
   {
      p[*num_items] = a;
      *num_items += 1;
   }
}

从静态数组中删除需要一个 for 循环,它将上面的项目向下移动一个并递减 int 以跟踪项目的数量。

void delete_item(struct orderSlip *p,int *num_items, int item)
{
   if (*num_items > 0 && item < *num_items && item > -1)
   {
      int last_index = *num_items - 1;
      for (int i = item; i < last_index;i++)
      {
         p[i] = p[i + 1];
      }
      *num_items -= 1;
   }
}

您可以通过将结构传递给执行该工作的函数来简化打印结构。

void print(const struct orderSlip  *p);

void print(const struct orderslip s);

可选

void print(const struct orderslip s, FILE *fp);

void print(const struct orderslip *p, FILE *fp)
{
   fprintf(fp,"%s\n",p->source);
    ...
}

void print_all(const struct orderSlip *p, int num_items)



//global
struct orderSlip data[MAX_ITEMS];
int num_items = 0;



int main(void)
{
...
       print_all(data,num_items);                                       
       strcpy(a.source,"source 2");
       strcpy(a.destination,"destination 20");
       strcpy(a.item,"item xyz");
       a.position = 99;
       add_item(data,a,&num_items);
       print_all(data,num_items);
       delete_item(data,&num_items,0);
       print_all(data,num_items);

【讨论】:

  • delete_item(data,&num_items,0); 0 是要移除的元素的位置;我可以删除位于其他位置的任何元素(1、2、3、4 等),但如果我尝试删除 3 个中的第一个数组结构,则最后 2 个与元素 2 相同(我正在使用 qsort()如果有帮助,位置是关键)
  • 编辑:我发现了错误:p[item] = p[item + 1];假设是 p[i] = p[i + 1];现在它正在正确删除第一个元素!谢谢
  • @user1615805 很高兴你找到我的错误。这就是我只用几个项目进行测试的结果......
  • @user1615805 看起来 delete_item() 中至少有两个错误。 for 循环太高了。它应该停在 (size - 2),而不是 (size - 1)。
  • 实际上,它已经停止在 size - 2,这就是 "
【解决方案2】:

一种方法是分配数组中的每个元素并只保留一个指针数组

struct orderSlip **data;

data = calloc(100, sizeof(struct orderSlip*)); // 100 pointers to struct

(calloc 将确保内存为零:从头开始)

每次添加新结构时:

data[i] = calloc(1, sizeof(struct orderSlip));

当你不再需要时

free(data[i]);

你也可以使用realloc来改变数据的大小,see

如果您不需要使用索引访问数组,您可以考虑另一种类型的数据结构,如链表,是真正动态的。

【讨论】:

  • 谢谢你,但是看起来这比我想象的要困难,我已经有一个指定数量的最大项目,并且将使用它而不是动态分配。不过感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 2021-07-22
  • 2022-01-23
  • 2017-01-30
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多