Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: printing members of a structure |
| Date | 2015-02-27 11:25 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <clb2h9F1vusU1@mid.uni-berlin.de> (permalink) |
| References | <mcop3s$953$4@dont-email.me> <claajmFr6qaU1@mid.individual.net> <mcp9ep$vq$1@dont-email.me> <mcp9lu$g35$1@dont-email.me> <mcphds$vq$3@dont-email.me> |
bilsch <king621@comcast.net> wrote:
> > If the following doesn't work...
> >
> > SDL_VideoInfo *p = SDL_GetVideoInfo();
> >
> > if(p != NULL)
> > {
> > printf("hw_available: %lu\n", p->hw_available);
> > }
> > else {
> > fprintf(stderr, "It's very dark in here\n");
> > }
> It works. Let me tell what I see:
> First line declares a pointer to type SDL_VideoInfo and initializes it to
> the value returned by SDL_GetVideoInfo. p is an address.
It's more than an address, it's a pointer to a 'SDL_VideoInfo'
structure, i.e. it comes with the additional information about
the type of what's stored at that address.
> Next check if p is something besides NULL.
> If yes get 32 bits from hw_available and print as long unsigned integer.
Mostly correct. But here's nothing that implies "32 bits". If
an unsigned long actually has, for example, 64 bits, it will
try to extract those 64 bits - it depends on the system this
is running on if an unsigned long as 32 bits or more. And
passing something to printf() that has a different size than
what you promised in the format string isn't a good idea,
since printf() can not detect any deviations.
If the structure element is (as it seems), 32 bit value and
you want to make sure it gets printed correctly on all systems
you either can cast the value to an unsigned long to make sure
it gets converted to an unsigned long before it's being passed
to printf()
printf("hw_available: %lu\n", (unsigned long) p->hw_available);
(note: an unsigned long will always suffice for a 32 bit
value since the standard requires that (unsigned) longs
have at least 32 bits) or you could use the C99 format
specifier macro defined in "inttyes.h":
#include <inttypes.h>
printf("hw_available: %PRIu32"\n", p->hw_available);
if your compiler is C99 compliant.
> I have never seen the notation -> before (except Mr. Schwarz post).
It's the usual notation to access structure (or union) members
when one has a pointer to one. You can replace it by
(*p).hw_available
(i.e. dereference the pointer, so you can use the "normal"
structure access operator ".") if you don't like it, but
you will find the "->" operator in nearly every program
using poiners to structures (or unions), so better get
used to it;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 03:42 +0000
Re: printing members of a structure Ian Collins <ian-news@hotmail.com> - 2015-02-27 17:37 +1300
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 08:21 +0000
Re: printing members of a structure Richard Heathfield <rjh@cpax.org.uk> - 2015-02-27 08:26 +0000
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 10:37 +0000
Re: printing members of a structure Barry Schwarz <schwarzb@dqel.com> - 2015-02-27 03:16 -0800
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 12:52 +0000
Re: printing members of a structure jt@toerring.de (Jens Thoms Toerring) - 2015-02-27 11:25 +0000
Re: printing members of a structure jt@toerring.de (Jens Thoms Toerring) - 2015-02-27 11:31 +0000
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 22:58 +0000
Re: printing members of a structure Keith Thompson <kst-u@mib.org> - 2015-02-27 08:20 -0800
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 22:55 +0000
Re: printing members of a structure Barry Schwarz <schwarzb@dqel.com> - 2015-02-26 22:03 -0800
Re: printing members of a structure Chad <cdalten@gmail.com> - 2015-02-26 23:06 -0800
Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 08:23 +0000
Re: printing members of a structure Nick Bowler <nbowler@draconx.ca> - 2015-03-02 18:40 +0000
Re: printing members of a structure James Kuyper <jameskuyper@verizon.net> - 2015-03-02 14:28 -0500
Re: printing members of a structure Nick Bowler <nbowler@draconx.ca> - 2015-03-03 15:07 +0000
csiph-web