The problem in short
I update global array of structures in one file (add_product.c
), but can't print these updates out in another file (show_list.c
).
Details
If you need a full image, look at github repo
Here is the essence.
The program I write let user does the following:
- Create a list of products
- Add a product on existing list
- Show all the products on the list
... and consists of 4 files:
global-variables.h
main.c
show_list.c
add_product.c
global-variables.h
#define LIST_OF_PRODUCTS_INITIAL_LENGTH 50
struct Product {
char name[50];
float price;
unsigned int amount;
};
extern struct Product *list_of_products[LIST_OF_PRODUCTS_INITIAL_LENGTH];
extern unsigned int current_list_of_products_length;
main.c
struct Product *list_of_products[LIST_OF_PRODUCTS_INITIAL_LENGTH];
unsigned int current_list_of_products_length = 0;
add_product();
show_list();
add_product.c
int add_product() {
...
struct Product new_product;
list_of_products[current_list_of_products_length] = &new_product;
current_list_of_products_length++;
// For example, cheese
scanf("%s", &list_of_products[current_list_of_products_length]->name);
fflush(stdin);
scanf("%f", &list_of_products[current_list_of_products_length]->price);
fflush(stdin);
printf("Enter the amount of %s: ", list_of_products[current_list_of_products_length]->name);
scanf("%d", &list_of_products[current_list_of_products_length]->amount);
fflush(stdin);
list_of_products_exists = true;
// **NOTICE**! All the values here are printed correctly (cheese 15 150)
printf("Name: %s \n", list_of_products[current_list_of_products_length]->name);
printf("Price: %.2f\n", list_of_products[current_list_of_products_length]->price);
printf("Amount: %d\n", list_of_products[current_list_of_products_length]->amount);
...
};
show_list.c
THE PROBLEM IS HERE!
int show_list() {
...
current_product = *list_of_products[0];
// EXPECTED cheese 15 150
// ACTUAL @6є 0 0.00
printf("%10s %10d %10.2f", current_product.name, current_product.amount, current_product.price);
...
}