How to encode a memory diagram?

I was asked to encode this memory diagram:

memory chart

I was also provided with this structure:

struct product {
    char *name;
    double price;
    int stock_count;
};
typedef struct product PRODUCT;

I do not need to write the full function only the required instructions and write an array from the heap that can store two products, with the first product being initialized as shown; that is, with information about the ends.

this is what i tried to do: for the element

PRODUCT item;
item->name = "bread"; //not sure if it "bread" or &"bread"
item->price = 2.25;
item->stock_count = 45;

for a heap array that can store 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'm not sure how to change the reading [0] on the memory chart, where the name is “pie”, the price is 9.50, and share_count is 7.

and, if possible, someone can write from the memory diagram what happens to it if

PRODUCT *p;
p = &inventory[1];
*p = item;

It was performed?

+4
2

, , , 1 inventory, p. ,

PRODUCT *inventory = NULL;
inventory  = malloc(sizeof(PRODUCT)*2);
assert(inventory !=NULL);

inventory ,

inventory[0].name = "pie";
inventory[0].price = 9.50;
inventory[0].stock_count = 7;
. . . . 
inventory[1].name 
inventory[1].price

.

+3

/:

inventory[0].name = "pie";
...

:

PRODUCT *p = &inventory[0];
p->name = "pie";
...

, , , : ( , )

PRODUCT inventory[2] = {
    { name: "pie", price: ..., stock_count: ... },
};
0

Source: https://habr.com/ru/post/1620672/


All Articles