[英]Struct array with array inside it
下面是在 C 中的结构体内部创建结构体数组的简短示例。我将其设为结构体侧面的结构体数组,因为我认为您可能想要自定义每个“杂货”/“价格”/ “单价”是。
在下面的代码中,我使用“malloc”来动态分配结构数组。 这是动态分配内存的“C”风格方式。 如果您使用的是 C++,那么您应该使用“new”
我只用 [0] 分配数组中的第一项。 您可以分别使用 [1] 和 [2] 索引分配其他人。
struct Grocery
{
/* fill in details of Grocery here you could add more than 1 item*/
int Value;
}
struct Quantity
{
/* fill in details of Quantity here */
int Value;
}
struct Unitprice
{
/* fill in details of Unitprice here */
int Value;
}
struct file
{
struct Grocery* groceries;
int totalGroceries;
struct Quantity* quantities;
int totalQuantities;
struct Unitprice* unitprices;
int totalUnitprices;
}
int main()
{
struct file Table;
Table.totalGroceries = 3
Table.groceries = (struct Grocery*)malloc(Table.totalGroceries * sizeof(Grocery));
Table.totalQuantities = 3
Table.quantities = (struct Quantity*)malloc(Table.totalQuantities * sizeof(Quantity));
Table.totalUnitprices = 3
Table.unitprices = (struct Unitprice*)malloc(Table.totalUnitprices * sizeof(Unitprice));
/* Assign details to groceries here */
Table.groceries[0].Value = 5;
/* Assign details to Quantity here */
Table.quantities[0].Value = 5;
/* Assign details to unitprices here */
Table.unitprices[0].Value = 5;
/* Other logic here */
/* End of program deallocate allocated memory */
free(Table.groceries);
free(Table.quantities);
free(Table.unitprices);
}
有关更多信息,我建议您查看此链接https://stackoverflow.com/questions/260915/how-can-i-create-a-dynamically-sized-array-of-structs#:~:text=If%20you %20want%20to%20allocate,type%20void%20(%20void*%20)关于如何在 C 中动态分配结构数组
虽然您的绘图和您的描述是在冲突在一定程度上,它似乎你想了许多项目或文件的能够存储数据,其中每个项目协调一个grocery
值, quantity
值和unitprice
值(类似于您将用作库存系统的基础)。
从您的绘图中,您的Filenr
结构成员没有多大意义。 为什么? 如果你的基础Item
结构坐标grocery
, quantity
和unitprice
,那么除非Filenr
提供的信息的附加唯一的位即是,部分grocery
, quantity
和unitprice
,那么它可以简单地省略,你可以使用它的索引来形容在grocery
, quantity
和unitprice
。
此外,作为char
类型,其值将被限制为 256 个值之一( -128 - 127
)。 如果您实际上将其视为可打印字符,那么您的值范围将减少到仅 96 个可打印字符。
您可以根据您拥有的数据定义结构。 如果Filenr
是一块数据的是你的输入的一部分,并且相关联grocery
, quantity
,以及unitprice
,然后将其添加到你的结构作为成员。 如果您使用的是指一种独特的结构,则不需要它只是东西-除非-你可以有两种结构在完全相同的值grocery
, quantity
和unitprice
和你正在使用Filenr
消除歧义在两个其他相同的结构之间。 否则,省略它并使用索引。
没有它,您只需创建一个结构数组。 这将使您能够按任何成员值进行排序、查询和求和。 打印存储(虚构)值的简单实现是:
#include <stdio.h>
typedef struct { /* your struct using a typedef for convenience */
int grocery, quantity;
double unitprice;
} item;
/* simple function to output all n struct in any array of item */
void prn_items (item *items, size_t n)
{
for (size_t i = 0; i < n; i++)
printf ("\nFilenr[%2zu]:\n grocery : %d\n quantity : %d\n unitprice : %0.4f\n",
i, items[i].grocery, items[i].quantity, items[i].unitprice);
}
int main (void) {
item files[] = {{ 31756, 22, 1.3405 }, /* made-up example data for array */
{ 7818, 83, 2.4722 },
{ 17920, 63, 1.3795 },
{ 2937, 32, 2.8648 },
{ 8423, 44, 2.6031 }};
size_t n = sizeof files / sizeof *files; /* number of elements in array */
prn_items (files, n); /* output all struct */
}
示例使用/输出
运行程序简单地将所有存储的由索引协调的结构值输出为Filenr
:
$ ./bin/grocerystruct
Filenr[ 0]:
grocery : 31756
quantity : 22
unitprice : 1.3405
Filenr[ 1]:
grocery : 7818
quantity : 83
unitprice : 2.4722
Filenr[ 2]:
grocery : 17920
quantity : 63
unitprice : 1.3795
Filenr[ 3]:
grocery : 2937
quantity : 32
unitprice : 2.8648
Filenr[ 4]:
grocery : 8423
quantity : 44
unitprice : 2.6031
现在,如果您打算将每个结构保存在单独的文件中,您可以简单地使用sprintf()
创建一个输出文件名,该文件名包含基于索引的唯一后缀(或前缀)。
如果你真的想要一个单一的char
和Filenr
作为你结构的一部分,你可以简单地通过将它作为成员添加回来来包含它,例如
typedef struct { /* your struct using a typedef for convenience */
char Filenr;
int grocery, quantity;
double unitprice;
} item;
调整代码提醒处理新增成员。
最后,您不想使用与价格相关的浮点数。 (当你因为舍入错误而亏钱时,公司会生气)。 最好使用相应乘以的整数值以确保不会发生舍入错误。 您可以搜索“货币的浮点类型”并找到有关该主题的大量附加信息。
仔细检查一下,如果您还有其他问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.