Hello,
I would like to save some values in a structure named Test
The structures is
TYPE test :
STRUCT
generalinfo :general; // another structure with 2 variables as string
testfreatures : freatures; // another structure with 2 variables as string
END_STRUCT
END_TYPE
I'm using a pointer to the structure test
VAR
INIValues1 : POINTER TO ARRAY [0..1000] OF STRING;
stTest : test;
END_VAR
##Program
INIValues1 := ADR(stTest);
// I want to write the first value of the first structure inside test as follow
INIvalues1^[0].^[0] := 'Test';
How can I mange to do this... I would like later only to replace the struct so my function fits for many different structures.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As long as all your structures contain elements of the same type, and the second array index of the ArrayOfStruct pointer is the same as the total number of elements in TEST_STRUCT (including all sub-structs), then you should be able to access everything like this:
ArrayOfStruct^[array index of TestStruct, element number relative to TEST_STRUCT]
Sorry, I posted part of my answer first on another thread but I wanted to post it here.
To achieve what you want, you do not declare a "POINTER TO ARRAY[] OF STRING". You declare a "POINTER TO STRING". You can use the array subscript notation directly with this pointer. The array subscript notation performs dereferencing, just like using "^". In fact, ptr[0] and ptr^ are equivalent as far as I can tell.
Please note that if, instead of STRING, you are using a structure as a type, you can access members by using "." right after the array subscript, like this :
ptr[0].member_1 := ...;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I cannot figure out exactly what your are trying to achieve, there is too much that is missing or incorrect in your code. I am under the impression that the best way to solve your problem is fundamentally different from what you are expecting it to be.
I am especially puzzled why you are trying to use a doubly-nested FOR loop in order to initialize a single structure, but maybe it is because I am inferring that from separate code blocks that are not really related.
A structure is not an array of strings, you cannot generically access structure members, and you cannot treat a structure as an array of strings in the general case.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I would like to save some values in a structure named Test
The structures is
TYPE test :
STRUCT
generalinfo :general; // another structure with 2 variables as string
testfreatures : freatures; // another structure with 2 variables as string
END_STRUCT
END_TYPE
I'm using a pointer to the structure test
VAR
INIValues1 : POINTER TO ARRAY [0..1000] OF STRING;
stTest : test;
END_VAR
##Program
INIValues1 := ADR(stTest);
// I want to write the first value of the first structure inside test as follow
INIvalues1^[0].^[0] := 'Test';
How can I mange to do this... I would like later only to replace the struct so my function fits for many different structures.
I think I understand what you're trying to do. It caught my attention because I've done something similar. I set up a test like this:
As long as all your structures contain elements of the same type, and the second array index of the ArrayOfStruct pointer is the same as the total number of elements in TEST_STRUCT (including all sub-structs), then you should be able to access everything like this:
ArrayOfStruct^[array index of TestStruct, element number relative to TEST_STRUCT]
Last edit: tvm 2022-01-27
Wow! TVM thank you very much this solves exactly what I am trying to do!.
I really appreciate your post... my code was as follow
Thank you guys for the support and the quick reply
Sorry, I posted part of my answer first on another thread but I wanted to post it here.
To achieve what you want, you do not declare a "POINTER TO ARRAY[] OF STRING". You declare a "POINTER TO STRING". You can use the array subscript notation directly with this pointer. The array subscript notation performs dereferencing, just like using "^". In fact, ptr[0] and ptr^ are equivalent as far as I can tell.
Please note that if, instead of STRING, you are using a structure as a type, you can access members by using "." right after the array subscript, like this :
ptr[0].member_1 := ...;
Thanks fajean,
But probably i need to explain my aplication a little bit more.
following ur example:
ptr[0].member_1 := ...; I wanted to do ".member" a variable so this way i can fill my structures no mater the size
Let's say:
I wanted to change only the structure pointer so i can fill many different structure with the same program.
Will this still be posible with the pointer that you propose?
Regards, and thank you for your fast reply
Thanks fajean,
But probably i need to explain my aplication a little bit more.
following ur example:
ptr[0].member_1 := ...; I wanted to do ".member" a variable so this way i can fill my structures no mater the size
Let's say:
I wanted to change only the structure pointer so i can fill many different structure with the same program.
Will this still be posible with the pointer that you propose?
Regards, and thank you for your fast reply
I cannot figure out exactly what your are trying to achieve, there is too much that is missing or incorrect in your code. I am under the impression that the best way to solve your problem is fundamentally different from what you are expecting it to be.
I am especially puzzled why you are trying to use a doubly-nested FOR loop in order to initialize a single structure, but maybe it is because I am inferring that from separate code blocks that are not really related.
A structure is not an array of strings, you cannot generically access structure members, and you cannot treat a structure as an array of strings in the general case.