#: 17213 S3/Languages
04-Dec-92 20:14:27
Sb: #17210-Structures in C-OS9 LII
Fm: Bruce MacKenzie 71725,376
To: Jon Beach 70004,1607 (X)
Jon,
Ok, first your declaration, 'struct sgtbuf *timeinfo;' , declares a
pointer to a structure (which is not what you really want--more on this la
later To access a structure element via a pointer you use the '->'
dereferencing opperator not the period. Thus you should use 'month =
timeinfo->t_month' not 'month = timeinfo.t_month'. This is the source of your
compiler errors, the compiler is looking for a structure named timeinfo whereas
you declared a structure pointer.
Anyway, when a structure pointer is declared the compiler only sets aside
memory for a pointer, it does not set aside memory for the structure.
Furthermore, until it is initialized it will contain garbage. If you call
gettime() with an unitialized pointer, gettime will store the structure data at
some random point in memory--a very dangerous proposition.
So, you want to declare a structure as in the Kreider example. This will
set aside variable memory large enough to receive the data from gettime(). But
gettime() expects a pointer to the structure as a parameter, not the structure
itself (which C won't let you do anyway--the source of your second compiler
error). The way to generate a pointer to a variable is to use the '&'
referencing opperator. Thus use 'gettime( &timeinfo );' to call the function.
If this business of pointers is making your head spin, be reassured
pointers give most everybody fits at first. They're neat once you catch on to
them though--they're the source of much of the power and flexibility of C.
|