【发布时间】:2016-11-22 22:20:07
【问题描述】:
我正在尝试将新项目添加到 GI 数组,但这似乎不起作用。 我在这里撞了一堵砖墙。有人可以建议吗? 我尝试如下所示传递 additem(GI, &Recs, 222) 并更新值:
#include <stdio.h>
struct Item {
double value;
int unitno;
int isTa;
int quant;
int minQuan;
char name[31];
};
struct Item GI[21] = {
{ 41.4,1275,01,110,12,"Apples" },
{ 52.99,3826,02,220,24,"Melon" },
};
int Recs=20;
void additem(struct Item item[], int *Recs, int unit);
void addtest();
int main ()
{
addtest();
return 0;
}
void addtest() {
additem(GI, &Recs, 222);
}
void additem(struct Item item[], int *Recs, int value)
{
printf("--== Adding values! ==--\n");
GI[21].value=44.44;
GI[21].quant=44;
GI[21].minQuan=4;
strcpy(Recs->name, "vGrape");
return 0;
}
【问题讨论】:
-
越界写入,未定义行为。
-
您的 Item 数组的大小是多少?尝试计算数组中使用的空格数,并使用它来决定将插入新条目的位置。
-
@johnelemans 我想我不明白你的意思,插入一个数组?
-
strcpy(Recs->name, "vGrape");:Recs的类型是int* -
additem 应该将一个新项目插入到您称为 GI 的数组中。