【问题标题】:c program fails when trying to print value using pointer尝试使用指针打印值时c程序失败
【发布时间】:2015-05-22 22:08:21
【问题描述】:

当我尝试从数组中获取值时,我没有收到任何错误,但程序失败了。该程序包含一个从文件中读取产品并将它们存储在typedef structure item 类型数组中的函数。

程序是这样的:

item *displayProducts(int balance){
    int row=0;
    char line[MAX_LINE_SIZE + 1]; // ptr to the current input line
    static item products[8];
    FILE *fp;

    fp = fopen("machinedata.txt", "r");
    if (fp == NULL)
    {
        printf("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    while (fgets(line, MAX_LINE_SIZE, fp)) {

        char *next_ptr = NULL;
        char *next_item = strtok_s(line, ",;", &next_ptr);

        while (next_item != NULL){
            char *item_ptr = NULL;
            char *name = strtok_s(next_item, "-", &item_ptr);
            if (name == NULL)
            {
                fprintf(stderr, "Failed to scan name out of [%s]\n", next_item);
                break;
            }
            int price;
            next_item = strtok_s(NULL, " ,", &item_ptr);
            //assert(next_item != NULL);
            if (strcmp(name," ")){
                if (sscanf(next_item, "%d", &price) != 1)
                    fprintf(stderr, "Failed to convert [%s] to integer\n", next_item);
                else if (balance > price){
                    products[row].name = name;
                    products[row].price = price;
                    products[row].product_code = row + 1;
                    printf("%d) %s:%d\n",products[row].product_code, products[row].name, products[row].price);
                    row++;
                }
                next_item = strtok_s(NULL, ",;", &next_ptr);
            }
        }
    }
    fclose(fp);
    return products;
}
void main( int argc, char *argv[]){

    int *ptr_to_balance;
    int balance = atoi(argv[2]);
    ptr_to_balance = &balance;
    item *ptr_to_products;

    Init(argv[1], balance);
    ptr_to_products = displayProducts(balance);
    printf("%s", *(ptr_to_products[2].name));

}

程序将打印出文件中的所有产品,但由于某种原因,程序的最后一行失败了。知道为什么吗?

【问题讨论】:

  • 欢迎来到 StackOverflow。然而,我们需要处理大量的代码!你能调试它直到你发现它出错的地方吗?您最终可能会自己回答问题,但如果没有,您将能够告诉我们哪里出了问题。但也要告诉我们症状,因为只是说“程序失败”并不能告诉我们实际出了什么问题。做这一切,你可能会从人们那里得到一些答案。
  • 你能给我们项目结构吗?它会帮助我们
  • typedef struct item{ char* name; int product_code; int price; }item;
  • 发布的代码无法编译。它缺少一些关键项目,例如“#include”语句。请重新发布代码的可编译版本,以演示潜在问题。然后告诉我们问题的症状是什么,以便我们为您提供帮助。如目前所写,编译步骤引发 42 个警告和错误!!!编译时,启用所有警告,(对于 gcc 使用,至少需要 '-Wall -Wextra -pedantic')
  • 在使用任何 'expected' 命令行参数之前,始终检查这样的参数是否存在,使用 main() 的 'argc' 参数。如果没有足够的参数(或参数类型错误),则打印“使用”语句并退出。

标签: c arrays pointers printf


【解决方案1】:

我认为,你需要改变

 printf("%s", *(ptr_to_products[2].name));

printf("%s", ptr_to_products[2].name);

因为 %s 需要一个 指向空值终止的指针 char 数组。

【讨论】:

  • 每个数组单元格中的值都在不断变化......我如何以不会改变的方式定义产品数组?感谢您的帮助
【解决方案2】:

products 数组中的所有指针都指向line 数组。这有两个问题:

  1. 这个数组是displayProducts的本地数组,当函数返回时它会被销毁。

  2. products 的每个元素都有指向同一行数组的指针。因此,当您从文件中读取新行时,您将覆盖保存在 products 先前元素中的值。

您需要先在堆中复制name,然后再将其保存到products[row]

    char *name_copy = malloc(strlen(name)+1);
    strcpy(name_copy, name);
    products[row].name = name_copy;

您还需要修复打印代码,如另一个答案:

printf("%s", ptr_to_products[2].name);

【讨论】:

  • 我试过了,但我收到了一个错误,提示我无法将 void* 转换为 char*
  • @DonovenRally:明确地说:那么您正在使用 C++ 编译器。
  • @DonovenRally 或者您对支持两者的编译器(例如 GCC)使用了错误的选项。您需要使用告诉它在 C 模式而不是 C++ 模式下编译的选项。我认为大多数编译器会自动从文件扩展名中找出这一点,例如.c 用于 C,.cpp 用于 C++。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 2011-01-29
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
相关资源
最近更新 更多