【发布时间】:2019-10-22 13:00:53
【问题描述】:
struct item {
char name[100];
double price;
} item;
char name[100];
int shelf;
int slot;
float price;
int NumOfShelves = 50
int NumOfSlotsPerShelf = 2
struct item *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int));
//这是我的“数组列表”^ 我认为
printf("Add an item name);
scanf("%s", &name);
printf("Add an item price);
scanf("%f", &price);
printf("Add the shelf number of the item");
scanf("%d", &shelf);
printf("Add the slot number");
scanf("%d", &slot);
//在这一行。如何将项目添加到我的阵列中的该插槽和架子?
printf("Search for an item by first giving the shelf number:");
scanf("%d", &slot);
printf("Search by giving the slot number");
scanf("%d", &slot);
//如果arrayl包含位置
//打印该位置的商品名称和价格
else {
printf("None")
return 0;
}
【问题讨论】:
-
您的问题不清楚。你想要
struct item的数组吗?而且我在这里看不到任何二维数组...请阅读此内容:How to Ask 然后edit 您的问题并澄清。 -
int *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int));这不是数组吗?是的,我想要一个结构项目数组。我该怎么办?
-
是的,这或多或少是
int的数组,但不是struct items 的数组。请参阅下面的答案。 -
明确一点,
int *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int)); 这不是一个数组吗? 不,它是指向内存中特定位置的指针的集合。int array1[10][20] = {0};是C数组的一个示例。两者都允许使用索引符号来访问集合的元素,但只有一个是实际数组。 -
@johnjacob 请不要对已回答的问题进行重大更改。它使给出的答案无效,从而破坏了整个 QA 想法。如果您在使用答案中的建议后仍有问题,则应改写一个新问题。
标签: c