所以,我有这样的结构:
typedef struct{
int serialNumber;
char name[100];
float price;
int quantity;
}Products;
我动态创建了一个结构数组。
任务是“模拟”一家杂货店,用户能够添加和编辑商店出售的商品。以下代码 fragment 用于编辑结构数据。
void overwrite(Products store){
printf("Enter new serial number: ");
scanf("%d", &(store.serialNumber));
getchar();
printf("Enter new product name: ");
fgets(store.name, 100, stdin);
store.name[strlen(store.name)-1]='\0';
printf("Enter new product price: ");
scanf("%f", &(store.price));
printf("Enter new product quantity: ");
scanf("%d", &(store.quantity));
}
void editData(Products *store, int storeCapacity){ //storeCapacity needed to invoke printData(), assume a working code for the function.
int choice;
printData(store, storeCapacity);
printf("Enter slot number of product here: ");
scanf("%d", &choice);
overwrite(store[choice]);
}
这里有一个问题,即使这段代码有效,当我尝试打印数据时,数据显示应该被覆盖的值。我是不是忘记做某事了?我希望你能帮助我。
顺便说一句,我在 Android 手机上编码。
最佳答案
void overwrite(Products store){
C 是按值传递,您需要传递一个指向 Products
的指针(即 Products *store
)并修改 overwrite
调用相应地在 editData
中。
关于android - 如何覆盖结构数组元素中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270551/