how to make an array of structures ? and after how to modify it

14 views
Skip to first unread message

Timur

unread,
Aug 17, 2021, 1:07:41 PM8/17/21
to 4tH-compiler
Good day.

how to make an array of structures ? Let's say there is a food warehouse, I need to store nuts-500 (pieces), vegetable oil-20... the names of the products are unknown in advance (I can't know either the names of the products or their quantity at the compilation stage), but during the program (the program reads certain information from the file), it is necessary to store the name of the product and its quantity in the heap (analogous to the C language structures). That is, I need an array of such structures (product, quantity). It is also necessary to somehow be able to change the amount of a certain product after filling this array, let's say it suddenly became necessary to change the amount of oil, then how to do it?

Timur


The Beez

unread,
Aug 19, 2021, 2:59:46 PM8/19/21
to 4tH-compiler
include lib/ncoding.4th

( define the structure )
struct
  64 +field name
  nell +field quantity
end-struct /product

( define number of products)
500 constant #product

( define an array of structs)
#product /product * buffer: products does> swap /product * + ;

( now enter the values)
s" nuts"  0 product -> name place
500 0 product -> quantity n!

s" vegetable oil" 1 product -> name place
20 1 product -> quantity n!

Hans Bezemer

Reply all
Reply to author
Forward
0 new messages