简体   繁体   English

尝试使用指针打印值时,c程序失败

[英]c program fails when trying to print value using pointer

I don't get any error yet when I am trying to get value from array the program fails. 当我尝试从数组中获取值时,我还没有收到任何错误,程序失败了。 The program contains a function to read products from file and store them in array of of type typedef structure item . 该程序包含一个从文件中读取产品并将其存储在typedef structure item类型的数组中的功能。

This is how the program looks like: 该程序如下所示:

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));

}

the program will print out all of the products from the file but for some reason the last line of the program fails. 该程序将从文件中打印出所有产品,但是由于某种原因,该程序的最后一行失败。 Any idea why? 知道为什么吗?

I think, you need to change 我认为,你需要改变

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

to

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

as %s expects a pointer-to-null-terminated char array. 因为%s需要一个以null结尾的 char数组的指针

All the pointers in your products array point into the line array. products数组中的所有指针都指向line数组。 This has two problems: 这有两个问题:

  1. This array is local to displayProducts , and it's destroyed when the function returns. 该数组在displayProducts本地,当函数返回时销毁。

  2. Each element of products is has pointers to the same line array. products每个元素都有指向同一行数组的指针。 So when you read a new line from the file, you're overwriting the values that were saved in the previous elements of products . 因此,当您从文件中读取新行时,您将覆盖在products的先前元素中保存的值。

You need to make a copy of name in the heap before you save it in products[row] . 您需要先在堆中复制name ,然后再将其保存在products[row]

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

You also need to fix the printing code, as in the other answer: 您还需要修复打印代码,如其他答案所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM