digitalmars.D - Creating an array of structs, without a Class
- Kevin M <Kevin_member pathlink.com> Jan 25 2005
- Dave <Dave_member pathlink.com> Jan 25 2005
- "Ben Hinkle" <ben.hinkle gmail.com> Jan 25 2005
- Kevin M <Kevin_member pathlink.com> Jan 25 2005
Hello, how should I translate the following code from "Programming Windows, 5th
Ed" (by Charles Petzold) into D Language? The D Language Specification does not
show how to create an array of structures. In this Newsgroup, I have found
examples of creating a "dynamic array of structs" but they require using a
Class. Is there a way to do this without creating a Class. Thanks in advance.
Kevin M
/*-----------------------------------------------
SYSMETS.H -- System metrics display structure
-----------------------------------------------*/
#define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))
struct
{
int iIndex ;
TCHAR * szLabel ;
TCHAR * szDesc ;
}
sysmetrics [] =
{
SM_CXSCREEN, TEXT ("SM_CXSCREEN"),
TEXT ("Screen width in pixels"),
SM_CYSCREEN, TEXT ("SM_CYSCREEN"),
TEXT ("Screen height in pixels"),
} ;
Jan 25 2005
On Tue, 25 Jan 2005 22:13:54 +0000, Kevin M wrote:Hello, how should I translate the following code from "Programming Windows, 5th Ed" (by Charles Petzold) into D Language? The D Language Specification does not show how to create an array of structures. In this Newsgroup, I have found examples of creating a "dynamic array of structs" but they require using a Class. Is there a way to do this without creating a Class. Thanks in advance. Kevin M
I think this will cover what you'd want to do: import std.stream; struct S { char[] str; int idx; } void main() { S[] arr; for(int idx = 1; idx <= 10; idx++) { S s; s.str = "dynamic"; // assign to str s.idx = idx; // assign to idx arr ~= s; // concatenate 's' to 'arr' } foreach(S s; arr) // iterate through the dynamic array { stdout.writefln(s.str,": ",s.idx); } static S[] s1 = [ {"static",11} ,{"static",12} ,{"static",13} ,{"static",14} ,{"static",15} ,{"static",16} ,{"static",17} ,{"static",18} ,{"static",19} ,{"static",20} ]; // initialize a static array of S's S[] s2; foreach(int i, S s; s1) // iterate thru the static array { stdout.writefln(s.str,": ",s.idx); if(i % 2) { s2 ~= s; s2[length - 1].str = "what was " ~ s.str ~ " is now dynamic"; } } foreach(S s; s2) // iterate thru dynamic array { stdout.writefln(s.str,": ",s.idx); } }
Jan 25 2005
struct SysMetric
{
int iIndex ;
char * szLabel ;
char * szDesc ;
}
const int SM_CYSCREEN = 0; // whatever...
const int NUMLINES = 2;
SysMetric[NUMLINES] sysmetrics =
[
{SM_CYSCREEN,"SM_CXSCREEN","Screen width in pixels"},
{SM_CYSCREEN,"SM_CYSCREEN","Screen height in pixels"}
];
int main(){
printf("%s\n",sysmetrics[0].szDesc);
printf("%s\n",sysmetrics[1].szDesc);
return 0;
}
Jan 25 2005
Thanks Ben, that's what I was looking for. Kevin M In article <ct6n69$309d$1 digitaldaemon.com>, Ben Hinkle says...struct SysMetric { int iIndex ; char * szLabel ; char * szDesc ; } const int SM_CYSCREEN = 0; // whatever... const int NUMLINES = 2; SysMetric[NUMLINES] sysmetrics = [ {SM_CYSCREEN,"SM_CXSCREEN","Screen width in pixels"}, {SM_CYSCREEN,"SM_CYSCREEN","Screen height in pixels"} ]; int main(){ printf("%s\n",sysmetrics[0].szDesc); printf("%s\n",sysmetrics[1].szDesc); return 0; }
Jan 25 2005









Dave <Dave_member pathlink.com> 