【问题标题】:Output changes after scanfscanf 后的输出变化
【发布时间】:2020-05-16 22:02:38
【问题描述】:

我是 C 新手,我的代码在 scanf 之后发生了变化,我该怎么办? 这里是代码:

void initialize_product(product **product_lst) {
    int result, place, product_list_length = list_length(product_lst);
    char barcode[BARCODE_LENGTH + 1] = "";
    product *product1 = (product *)calloc(1, sizeof(product));
    if (product1 == NULL) {
        printf("Not enough memory");
        exit(1);
    }
    if (product_list_length >= MAX_NUM_PRODUCTS) {
        printf("%s\n", too_much_products);
    }
    (*product1).barcode = (char *)malloc(BARCODE_LENGTH + 1);
    if ((*product1).barcode == NULL) {
        printf("Not enough memory");
        exit(1);
    }
    (*product1).product_name = (char *)malloc(MAX_PRODUCT_NAME_LENGTH + 1);
    if ((*product1).product_name == NULL) {
        printf("Not enough memory");
        exit(1);
    }
    (*product1).product_catagory = (char *)malloc(MAX_CATEGORY_LENGTH + 1);
    if ((*product1).product_catagory == NULL) {
        printf("Not enough memory");
        exit(1);
    }
    (*product1).expire_date = (date *)malloc(sizeof(date));
    if ((*product1).expire_date == NULL) {
        printf("Not enough memory");
        exit(1);
    }
    printf("%s", adding_product_barcode);
    scanf("\n%s", &(*(*product1).barcode));
    printf("%s", &(*(*product1).barcode));
    //strcpy(&(*product1).barcode, &barcode);
    result = barcode_compare(product_lst, (*product1).barcode);
    if (result == 0) {
        printf("%s", adding_product_name);
        scanf("\n%[^\n]s", (*product1).product_name);
        printf("%s", adding_product_category);
        scanf("\n%[^\n]s", (*product1).product_catagory);
        printf("%s", adding_product_number);
        scanf("\n%d", &(*product1).available);
        printf("%s", &(*product1).barcode);
        printf("%s", adding_product_price);
        scanf("\n%lf", &(*product1).price);
        strcpy(&(*product1).barcode, &barcode);
        printf("%s", adding_product_date);
        scanf("\n%d/%d/%d", &(*(*product1).expire_date).day, &(*(*product1).expire_date).month, &(*(*product1).expire_date).year);
        printf("%d", (*product1).available);
        printf("The product %s -barcode:%s ,added successfully\n", &(*(*product1).product_name), &(*(*product1).barcode));
        product_lst[product_list_length] = product1;
        printf("%d", (*product1).available);
        printf("barcode is %s, name is %s, catagory is %s, price is %lf, available is %d",
               &(*product_lst[0]).barcode, &(*(*product_lst[0]).product_name),
               &(*(*product_lst[0]).product_catagory), (*product_lst[0]).price,
               (*product_lst[0]).available);
    } else {
        printf("Product already exist, please enter number of products to add: ");
        scanf("%d", &(*product1).available);
        place = barcode_place(product_lst, barcode);
        product_lst[place]->available = (*product1).available;
    }
}

scanf条码的内存发生变化后,怎么办? 我尝试向malloc/calloc 添加更多内存,但没有帮助,我真的不知道该怎么办。

【问题讨论】:

  • 你为什么用(*pointer1).available而不是pointer1->available?这在这里没有帮助。几乎没有人这样写。
  • After the scanf the memory of the barcode is changing 我不明白你的意思是什么?发生了什么变化?
  • "\n%[^\n]s 不会像您认为的那样做,因为"\n..." 永远不会匹配。为什么? '\n'whtespace 并且在 format string 中包含空格有什么作用? (提示:忽略空格)。你的整个输入程序比蛋壳还要脆弱。一个流浪字符,整个事情就爆炸了——你不知道在哪里——因为你没有检查你的输入函数的返回。如果不检查返回,您将无法正确使用任何输入函数....list_length(product_lst);product_lst 的间接级别上看起来也很可疑...
  • 我的内存大小有问题,数据似乎泄漏到其他地方,也有语法问题,但现在都解决了,请大家帮忙。

标签: c arrays pointers


【解决方案1】:

以下行可能是问题所在:

strcpy(&(*product1).barcode, &barcode);

您不需要&s,因为两个条形码都已经是char*。使用:

strcpy((*product1).barcode, barcode);

编辑

我认为您可能还混淆了strcpy 中的参数顺序。似乎(*product1).barcode 应该是源,而不是目标,所以正确的形式是:

strcpy(barcode, (*product1).barcode);

【讨论】:

  • 顺序是对的,我只需要删除 &,ty 很多!
猜你喜欢
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多