Is it possible to convert a simple structure to an uchar array?

 

I am using MQL4 v.840.

I made a simple struct (without stings, without dynamic arrays). I can write this structure to a file with FileWriteStruct(). This implies the content of the struct is converted to a sequence of uchar.

I it be possible to convert a structure to an uchar array in MQL4 without writing to a file?

 
Structures and Classes - MQL4 Documentation states
Structures that do not contain strings or objects of dynamic arrays are called simple structures; variables of such structures can be freely copied to each other, even if they are different structures.
So this should work.
struct actual{
   double D;
   int    I;
}
struct asUchar{
   uchar A[99];
}
:
actual v{ 1.0, 2};
asUchar c = v;
see also the example in the Typecasting - MQL4 Documentation
 
WHRoeder:
Structures and Classes - MQL4 Documentation states
Structures that do not contain strings or objectsof dynamic arrays are called simple structures; variables of suchstructures can be freely copied to each other, even if they are different structures.
Thank you very much. You really helped me a lot.
 

hello,

i did some tests and the solution described is not working. For it to work properly, you have to assign like that

asUchar c; c.A[0]=v.D; c.A[1]=v.I

 which is not something it can be looped In general, I do not think it can be done in MQL. In C you can do it with pointers, as described here http://cboard.cprogramming.com/c-programming/155277-looping-struct.html But in general, I consider structures brain damage for the most part :) I consider so, because most of the things we are trying to do with structures can be done faster with using arrays; faster because we may not use the logic overhead that is coming with the functions needed for the structures to work. Some may say that it is usefull to have names for the variables but I guess good documenting or commenting can do that as well as. Structures can be usefull as packed structures where someone could fit as much data as possible in a given space, a scenario which I believe is highly unlikely to happen in a personal project and given how big are RAM and storage today , using structures can be a waste of easiness of use, of writing and reading the code and a waste of cpu cycles as well

 

best regards 

 
Demos:

hello,

i did some tests and the solution described is not working. For it to work properly, you have to assign like that

 which is not something it can be looped In general, I do not think it can be done in MQL. In C you can do it with pointers, as described here http://cboard.cprogramming.com/c-programming/155277-looping-struct.html But in general, I consider structures brain damage for the most part :) I consider so, because most of the things we are trying to do with structures can be done faster with using arrays; faster because we may not use the logic overhead that is coming with the functions needed for the structures to work. Some may say that it is usefull to have names for the variables but I guess good documenting or commenting can do that as well as. Structures can be usefull as packed structures where someone could fit as much data as possible in a given space, a scenario which I believe is highly unlikely to happen in a personal project and given how big are RAM and storage today , using structures can be a waste of easiness of use, of writing and reading the code and a waste of cpu cycles as well

 

best regards 

It's working properly for me.

What's your goal ? Maybe there is an other solution.

I totally disagree about what you wrote on structure, but that's an other matter.

 
zirkoner:

It's working properly for me.

What's your goal ? Maybe there is an other solution.

I totally disagree about what you wrote on structure, but that's an other matter.

I am not the OP :) Sooo.... I am wondering how is it working for you? If I enter the code as such (I add semicolons, Alert etc. )

struct actual{
   double D;
   int    I;
} ;

struct asUchar{
   uchar A[99];		
} ;

actual v{ 1.0, 2};
asUchar c = v;
Alert(c.A[0]," ",c.A[1]);

it prints "0 0" 

 
Demos:

I am not the OP :) Sooo....

Ah yes, I missed that.

I am wondering how is it working for you? If I enter the code as such (I add semicolons, Alert etc. )

it prints "0 0" 

It prints 0 0 and what ?

uchar is 1 byte, a double is 8 bytes and an int is 4. Why are you expecting a value other than 0 in A[0] and A[1] ? there will be 12 items in A[].

 

ah alright, it does not do a cast then (i should have read the links in here :) )

 

best regards 

Reason: