Initialising a items in a structure
-
I wist to initialise some items in a structure, but the only way I can see to do it is as follows:-
// General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc
Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:-// General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01;
etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement likestrcpy(thisItem.details.fieldDesc,"The field description");
Are they any other ways to load this constant data? Graham. grahamfff -
I wist to initialise some items in a structure, but the only way I can see to do it is as follows:-
// General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc
Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:-// General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01;
etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement likestrcpy(thisItem.details.fieldDesc,"The field description");
Are they any other ways to load this constant data? Graham. grahamfffWell you can always do
typedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA;
Then every new GENERAL_DATA variable will be initialized. -
I wist to initialise some items in a structure, but the only way I can see to do it is as follows:-
// General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc
Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:-// General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01;
etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement likestrcpy(thisItem.details.fieldDesc,"The field description");
Are they any other ways to load this constant data? Graham. grahamfffIf this is C++, you should add constructors to both
struct
's to handle the initialization. If this is 'plain old' C (which I guessed might be the case, from your use oftypedef
), you can try the following:// General part of the Structure (used many times)
typedef struct
{
char fieldDesc [100];
char detailsDesc [20];
char unitsStr [10];
} GENERAL_DATA;
// The main structure
typedef struct
{
unsigned short data; // Dyanamic data
GENERAL_DATA details; // Static data
}ITEM_DATA;
static ITEM_DATA InitialItemData = {1234, // 'unsigned short data' in the ITEM\_DATA struct { "field description", // char fieldDesc\[100\] "detail", // char detailsDesc\[20\] "units" // char unitsStr\[10\] }
};
Anywhere you need to initialize an
ITEM_DATA
, just assign it:ITEM_DATA current_item;
current_item = InitialItemData;
Software Zen:
delete this;
-
Well you can always do
typedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA;
Then every new GENERAL_DATA variable will be initialized.Thanks for the info, that solves the first problem. But can you advise on how to use a
union
or other overlay method. I just want to deal with a buffer when sending out the data, but deal with the structure itmes in the program; i.e. two names for the same item of data; see listing belowtypedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; typedef struct { unsigned short data; GENERAL_DATA details; }ITEM_DATA; union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01;
I get the error c:\Overlays\OverlaysDlg.cpp(48): error C2620: member 'SIGNAL1::thisSignal' of union 'SIGNAL1' has user-defined constructor or non-trivial default constructor. Are they another method of overlaying the data. grahamfff -
If this is C++, you should add constructors to both
struct
's to handle the initialization. If this is 'plain old' C (which I guessed might be the case, from your use oftypedef
), you can try the following:// General part of the Structure (used many times)
typedef struct
{
char fieldDesc [100];
char detailsDesc [20];
char unitsStr [10];
} GENERAL_DATA;
// The main structure
typedef struct
{
unsigned short data; // Dyanamic data
GENERAL_DATA details; // Static data
}ITEM_DATA;
static ITEM_DATA InitialItemData = {1234, // 'unsigned short data' in the ITEM\_DATA struct { "field description", // char fieldDesc\[100\] "detail", // char detailsDesc\[20\] "units" // char unitsStr\[10\] }
};
Anywhere you need to initialize an
ITEM_DATA
, just assign it:ITEM_DATA current_item;
current_item = InitialItemData;
Software Zen:
delete this;
Thanks for the info and yes was for C code. Just two points, how would you do it for C++ and can you overlay the structure via union;
union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01;
i.e. same data by two different names. grahamfff -
I wist to initialise some items in a structure, but the only way I can see to do it is as follows:-
// General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc
Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:-// General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01;
etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement likestrcpy(thisItem.details.fieldDesc,"The field description");
Are they any other ways to load this constant data? Graham. grahamfff -
Thanks for the info and yes was for C code. Just two points, how would you do it for C++ and can you overlay the structure via union;
union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01;
i.e. same data by two different names. grahamfffFor C++, I would add a constructor. In C++,
class
andstruct
are the same except for one point. Withstruct
, members havepublic
access by default, while withclass
, the access isprivate
by default. This means that astruct
can have a constructor, and that's the place you should initialize the values. You can initialize theunion
as follows:data01.thisSignal = ItemDataInitial;
where
ItemDataInitial
is an initializedITEM_DATA
as I described in the previous post. If you use a constant initializer for theunion
, it must correspond to the first field in it.
Software Zen:
delete this;
-
For C++, I would add a constructor. In C++,
class
andstruct
are the same except for one point. Withstruct
, members havepublic
access by default, while withclass
, the access isprivate
by default. This means that astruct
can have a constructor, and that's the place you should initialize the values. You can initialize theunion
as follows:data01.thisSignal = ItemDataInitial;
where
ItemDataInitial
is an initializedITEM_DATA
as I described in the previous post. If you use a constant initializer for theunion
, it must correspond to the first field in it.
Software Zen:
delete this;
Thanks for the info. I have started to use your scheme, or a varient of it. Will use the union as follows:- typedef struct { unsigned short data1; }DATA1; typedef struct { unsigned : 4 data2; }DATA2; etc union ALL_DATA_UNION { DATA1 data1; DATA2 data2; etc unsigned short buffer[50]; }ALL_DATA; I need to transmit the buffer, but want all the data items 'overlayed' with the buffer. Dont need to initialise the union as can use memcpy to zero the buffer. I will have another structure with the details of each item of data and initialise it as per your scheme. Thanks again, but please point out if this scheme is fault. Regards, Graham grahamfff