Re: Structures Again?
- From: "Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 14 Sep 2005 16:44:03 -0700
Thanks David and Igor!
OKAY! So if we refer to the code below this paragrah, when the array
innitializes the values, it assigns an address to * szLabel which is a
pointer of type character whereby this address will be pointing to the
actuall location of were the string is immediately stored. This is indeed an
important aspect for a novice programmer to fully understand the automated
tasks that are performed behind the scenes! I am now satisfied with this
understanding!
struct
{
int iIndex;
const TCHAR * szLabel;
const TCHAR * szDesc;
}
sysmetrics [] =
{
SM_CXSCREEN, TEXT("SM_CXSCREEN"),
TEXT("Screen width in pixels"),
SM_CYSCREEN, TEXT("SM_CYSCREEN"),
TEXT("Screen width in pixels"),
};
However , one more thing.... since integer iIndex is not a pointer, I know
its supposed to simply store the SM_CXSCREEN variable. I would just like to
confirm, is SM_CXSCREEN an integer variable or a string? Because further
along in the program we use another TextOut function, this time using iIndex:
TextOut (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,
wsprintf (szBuffer, TEXT ("%5d"),
GetSystemMetrics (sysmetrics[i].iIndex)));
It would be nice if my book explanation would tell me what GetSystemMetrics
takes as an argument, in this case I am left to believe it takes an integer
since thats what is stored in sysmetrics[i].iIndex, but an integer is this
-2- and not this SM_CXSCREEN. So, if SM_CXSCREEN is an integer variable,
where is it defined in Windows that SM_CXSCREEN is supposed to hold the value
of lets say 3.
I am having a hard time putting it all together, but I am optimistic in
getting there!
David, Igor or anyone!!!
--
Best regards
Robert
"David Wilkinson" wrote:
> Robby wrote:
>
> > Hi Igor...
> >
> > So when you are innitializing the array, you are actually inniytializing it
> > with adresses so that when I execute lets say the following command in my
> > program, it will pass an address to point to the actual text:
> >
> > TextOut (hdc, 0, cyChar * i, sysmetrics[i].szLabel, lstrlen
> > (sysmetrics[i].szLabel));
> >
> > Sorry for my mis-understanding and I appreciate your help!
> > But how is "SM_CXSCREEN" an address? I mean what does exactly happen behind
> > the scenes when the parameter is passed in the above statement at
> > "sysmetrics[i].szLabel" it is "SM_CXSCREEN" right, and so this is passed in
> > to the TextOut, then what, how do the internal function's mecanics work so
> > that it interprets "SM_CXSCREEN" as an address to the actual text that will
> > be displayed which is "SM_CXSCREEN"?
> >
> > Please get back! Thanks!
> >
>
> Robbie:
>
> Your current question has nothing to do with arrays of structures, or
> even structures. It just has to do with the way string literals are
> handled by the C or C++ compiler. When you write
>
> char* str = "This is a string";
>
> the compiler sets aside a chunk of memory to hold the string, and sets
> str equal to its address. This is what happens when you initialize your
> structure, or array of structures.
>
> The memory assigned to the string is non-modifiable. You cannot use the
> above str pointer to modify it (using strcpy(), for example). But this
> error will only show up at run time (access violation). In fact, in C++
> it is better to write
>
> const char* str = "This is a string";
>
> Now the compiler will stop you from trying to modify the memory. So, you
> should define your struct as
>
> struct
> {
> int iIndex;
> const TCHAR * szLabel;
> const TCHAR * szDesc;
> }
> sysmetrics [] =
> {
> SM_CXSCREEN, TEXT("SM_CXSCREEN"),
> TEXT("Screen width in pixels"),
> SM_CYSCREEN, TEXT("SM_CYSCREEN"),
> TEXT("Screen width in pixels"),
> };
>
> HTH,
>
> David Wilkinson
>
>
>
>
.
- Follow-Ups:
- Re: Structures Again?
- From: David Wilkinson
- Re: Structures Again?
- References:
- Re: Structures Again?
- From: Jim Langston
- Re: Structures Again?
- Prev by Date: Re: throw()
- Next by Date: Re: Structures Again?
- Previous by thread: Re: Structures Again?
- Next by thread: Re: Structures Again?
- Index(es):
Relevant Pages
|