I also got this structure:
struct product {
char *name;
double price;
int stock_count;
};
typedef struct product PRODUCT;
I don’t have to write a complete function, I only need the required statements, and from the heap Write an array that can store two products. The first product is initialized as shown in the figure; that is, information about the pie.
This is what I tried to do:
For the project
PRODUCT item;
item->name = "bread"; //not sure if it's "bread" or &"bread"
item->price = 2.25;
item->stock_count = 45;
For the array in the heap that can be stored
Two products:
PRODUCT *inventory, *p;
p = malloc(sizeof(PRODUCT)*2);
assert(p!=NULL);
inventory = p; //I've also read that the answer might be p = inventory not sure why though
But I don’t know how to change the [0] program named “pie” in the memory map, the price is 9.50, and the stock_count is 7.
If possible, someone can write down what happens to it on the memory map
PRODUCT *p;
p = &inventory[1];< br />*p = item;
Is it executed?
PRODUCT *inventory = NULL ;
inventory = malloc(sizeof(PRODUCT)*2);
assert(inventory !=NULL);
Then you can use inventory to access members, for example
inventory[0].name = "pie";
inventory[0].price = 9.50;
inventory[0].stock_count = 7;
....
inventory[1].name
inventory[1].price
etc.
I was asked to write this memory diagram:
I also got this structure:
struct product {
char *name;
double price;
int stock_count;
};
typedef struct product PRODUCT;
I don’t have to write a complete function, only the required statements, and write an array that can store two products from the heap, The first product initialization is shown in the picture; that is, information about the pie.
This is what I tried to do:
For the project
PRODUCT item;
item->name = "bread"; //not sure if it's "bread" or &"bread"
item->price = 2.25;
item->stock_count = 45;
For the array in the heap that can be stored
Two products:
PRODUCT *inventory, * p;
p = malloc(sizeof(PRODUCT)*2);
assert(p!=NULL);
inventory = p; //I've also read that the answer might be p = inventory not sure why though
But I don’t know how to change the [0] program named “pie” in the memory map, the price is 9.50, and the stock_count is 7.
If If possible, someone can write what happens to it on the memory map
PRODUCT *p;
p = &inventory[1];
* p = item;
Is it executed?
If I understand your request correctly, then it seems that you are correct, and according to my understanding, if you only need 1 inventory , Then you don’t need p anyway. You can change your code
PRODUCT *inventory = NULL;
inventory = malloc(sizeof(PRODUCT )*2);
assert(inventory !=NULL);
Then you can use inventory to access members, for example
inventory [0].name = "pie";
inventory[0].price = 9.50;
inventory[0].stock_count = 7;
....
inventory[ 1].name
inventory[1].price
etc.