android - 如何覆盖结构数组元素中的数据?

标签 android c

所以,我有这样的结构:

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/

相关文章:

java - Gradle - 错误找不到参数的方法实现()[com.android.support :appcompat-v7:26. 0.0]

c++ - 扩展/重组宏元组

c# - 用于在构建期间将 C 枚举和常量脚本编写到 C# 的工具

c - 将整个输入保存在整数数组中(使用 getchar 读取)

c - 给定以下级数...级数的第 n 项等于 (n - 1)th ^2 +1 并且级数的第一项是 1 使用递归

android - 此 Android SDK 需要 Android Developer Toolkit 版本 22.0.0 或更高版本。当前版本是 21.x.x。

android - 图像尺寸太小 Azure Face API Android

java - 文件的加密/解密操作在 Android 平台上产生奇怪的效果

android - AndroidX 迁移后命令行 lint 错误

c - 使用 Microsoft Visual Studio '08 编译 libpng 时如何避免 "error LNK2005:"(已定义的 stdlib 函数)?