creating an array of structures
-
I am trying to create an array of structures. The structure that I have created looks like this:
struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; };
My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created:struct TSimHeader *header_ptr = new TSimHeader[1];
I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:) -
I am trying to create an array of structures. The structure that I have created looks like this:
struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; };
My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created:struct TSimHeader *header_ptr = new TSimHeader[1];
I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:)Use a CArray template: #include "afxtempl.h" . . . CArray arrayName; arrayName.SetSize(0,1); Then you can simly use the the memberfunctions of CArray to Add/Insert/Delete... elements of the type TSimHeader. Search MSDN for CArray for more info. Hope that helps MS
-
I am trying to create an array of structures. The structure that I have created looks like this:
struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; };
My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created:struct TSimHeader *header_ptr = new TSimHeader[1];
I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:)You can do something like:
struct TSimSignal
{
double Value[45];
int SimWriteFlag;
int data_index;
int DisplayReadFlag;
long TimeStamp;
} TSimSignalArr[10];or
struct TSimSignal
{
double Value[45];
int SimWriteFlag;
int data_index;
int DisplayReadFlag;
long TimeStamp;
} *TSimSignalArr;TSimSignalArr = new struct TSimSignal[10];
-
You can do something like:
struct TSimSignal
{
double Value[45];
int SimWriteFlag;
int data_index;
int DisplayReadFlag;
long TimeStamp;
} TSimSignalArr[10];or
struct TSimSignal
{
double Value[45];
int SimWriteFlag;
int data_index;
int DisplayReadFlag;
long TimeStamp;
} *TSimSignalArr;TSimSignalArr = new struct TSimSignal[10];
David, I must be doing something wrong because When I try that I get these errors: d:\.h(30) : error C2501: 'TSimHeader_arr' : missing storage-class or type specifiers d:\.h(30) : error C2040: 'TSimHeader_arr' : 'int' differs in levels of indirection from 'struct TSimHeader *' d:\.h(30) : error C2440: 'initializing' : cannot convert from 'struct TSimHeader *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast d:\.cpp(164) : error C2275: 'TSimHeader' : illegal use of this type as an expression d:\.h(20) : see declaration of 'TSimHeader' I don't know why I get the error about trying to convert a pointer to an int?
struct TSimHeader { char *Name[45]; char *Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }*TSimHeader_arr; TSimHeader_arr = new struct TSimHeader[10];
Once I have added all the data that I need to the TSimHeader struct I'm trying to put that struct in position 0.TSimHeader_arr[0] = TSimHeader;
Thanks David, your always helpful. sj:) -
David, I must be doing something wrong because When I try that I get these errors: d:\.h(30) : error C2501: 'TSimHeader_arr' : missing storage-class or type specifiers d:\.h(30) : error C2040: 'TSimHeader_arr' : 'int' differs in levels of indirection from 'struct TSimHeader *' d:\.h(30) : error C2440: 'initializing' : cannot convert from 'struct TSimHeader *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast d:\.cpp(164) : error C2275: 'TSimHeader' : illegal use of this type as an expression d:\.h(20) : see declaration of 'TSimHeader' I don't know why I get the error about trying to convert a pointer to an int?
struct TSimHeader { char *Name[45]; char *Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }*TSimHeader_arr; TSimHeader_arr = new struct TSimHeader[10];
Once I have added all the data that I need to the TSimHeader struct I'm trying to put that struct in position 0.TSimHeader_arr[0] = TSimHeader;
Thanks David, your always helpful. sj:)johnstonsk wrote: TSimHeader_arr[0] = TSimHeader; Your TSimHeader variable shouldn't be the same name as the structure. That aside, once you have the members of this variable set accordingly, you need to do individual assignments to TSimHeader_arr[0], like:
TSimHeader_arr[0].Min = TSimHeaderVar.Min; TSimHeader_arr[0].Max = TSimHeaderVar.Max; TSimHeader_arr[0].SignalCount = TSimHeaderVar.SignalCount; etc.