STRUCT_INSERT
Dexcription
The struct_insert
function is used to insert a new field into a structure (struct). The name of the new field is specified by the name
parameter, and its value is specified by the expr
parameter. The optional indexToInsert
parameter is used to specify the insertion position. If indexToInsert
is not specified or its value is 0, the new field will be inserted at the end of the structure. If indexToInsert
is 1, the new field will be inserted after the first field, and so on.
Functionality
- Add a new field to the structure.
- Control the insertion position of the new field through the
indexToInsert
parameter.
Parameters
struct
: The structure into which the new field needs to be inserted.name
: The name of the new field, must be a string constant.expr
: The value of the new field, can be data of any type.indexToInsert
: (Optional) Specifies the insertion position of the new field, an integer constant, default value is 0.
Return Result
Returns a new structure, the structure of which depends on the inserted key-value pair and the specified insertion position.
Usage Example
- Add a new field to the end of the structure:
- Insert new fields at the beginning of the struct:
- Insert a new field after the first field:
- Insert a new field before the second field:
- Insert new fields into a structure that contains nested structures:
Notes
- Please ensure the
name
parameter is a string constant, otherwise it may cause the function to fail. - The valid range for the
indexToInsert
parameter is from 0 to the number of struct fields (inclusive). Values outside this range will cause the function to fail. - When the insertion position conflicts with existing fields (for example, attempting to insert a new field at the position of an existing field), the function will return an error.