【发布时间】: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 中使用动态数组。