1

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.

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?

5
  • 1
    Welcome to StackOverflow. However, that's an awful lot of code for us to work through! Can you debug through it until you find where it's going wrong? You may end up answering the question yourself, but if not then you will be able to tell us where it's going wrong. But also tell us the symptom,as just saying " the program fails" doesn't tell us what's actually going wrong. Do all that, and you may get some answers from people. Commented May 22, 2015 at 22:16
  • could you give us the item structure? it will help us Commented May 22, 2015 at 22:22
  • typedef struct item{ char* name; int product_code; int price; }item; Commented May 22, 2015 at 23:33
  • the posted code does not compile. It is missing a few key items like '#include' statements. Please repost a compilable version of the code, that demonstrates the underlying problem. Then tell us what the symptoms of the problem are, so we can help you. As currently written, the compile step raises 42 warnings and errors!!! When compiling, enable all the warnings, (for gcc use, as a minimum, '-Wall -Wextra -pedantic') Commented May 23, 2015 at 22:50
  • before using any 'expected' command line parameter, always check if such a parameter exists, using the 'argc' parameter to main(). And if not enough parameters (or the parameters are of the wrong type) then print a 'usage' statement and exit. Commented May 23, 2015 at 23:00

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The value keep on changing in each of the array cells... how can i define the products array in a way it wont change??? thanks for the help
2

All the pointers in your products array point into the line array. This has two problems:

  1. This array is local to displayProducts, and it's destroyed when the function returns.

  2. Each element of products is has pointers to the same line array. So when you read a new line from the file, you're overwriting the values that were saved in the previous elements of products.

You need to make a copy of name in the heap before you save it in 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);

3 Comments

i tried that but i got an error about cant convert void* to char* that i couldn't overcome
@DonovenRally: To be explicit: You are using a C++ compiler then.
@DonovenRally Or you're using the wrong options to a compiler that supports both (e.g. GCC). You need to use the options that tell it to compile in C mode rather than C++ mode. I think most compilers figure this out automatically from the file extension, e.g. .c for C, .cpp for C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.