Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #57718 > unrolled thread
| Started by | bilsch <king621@comcast.net> |
|---|---|
| First post | 2015-02-27 03:42 +0000 |
| Last post | 2015-03-03 15:07 +0000 |
| Articles | 18 — 9 participants |
Back to article view | Back to comp.lang.c
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
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 03:42 +0000 |
| Subject | printing members of a structure |
| Message-ID | <mcop3s$953$4@dont-email.me> |
hypothetical function getstructure() returns a pointer to a structure of
the form:
struct{int one;int two;}strc1.
The structure name is already included via a header file so I cant use it
it the program. I want to do something like the following (which doesn't
work) but I don't know how to assign names to the structure members. TIA.
Bill S
[
printf("int1= %i int2= %i /n", strc1.one, strc1.two);
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-02-27 17:37 +1300 |
| Message-ID | <claajmFr6qaU1@mid.individual.net> |
| In reply to | #57718 |
bilsch wrote:
> hypothetical function getstructure() returns a pointer to a structure of
> the form:
>
> struct{int one;int two;}strc1.
>
> The structure name is already included via a header file so I cant use it
> it the program. I want to do something like the following (which doesn't
> work) but I don't know how to assign names to the structure members. TIA.
> Bill S
>
> [
> printf("int1= %i int2= %i /n", strc1.one, strc1.two);
You need to do a better job of explaining your problem. Some real (even
if it won't compile) code would help.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 08:21 +0000 |
| Message-ID | <mcp9ep$vq$1@dont-email.me> |
| In reply to | #57719 |
This is in the #include file SDL_video.h
typedef struct SDL_VideoInfo {
Uint32 hw_available :1;
Uint32 wm_available :1;
Uint32 UnusedBits1 :6;
Uint32 UnusedBits2 :1;
Uint32 blit_hw :1;
Uint32 blit_hw_CC :1;
Uint32 blit_hw_A :1;
Uint32 blit_sw :1;
Uint32 blit_sw_CC :1;
Uint32 blit_sw_A :1;
Uint32 blit_fill :1;
Uint32 UnusedBits3 :16;
Uint32 video_mem;
SDL_PixelFormat *vfmt;
int current_w;
int current_h;
} SDL_VideoInfo;
The function SDL_GetVideoInfo returns a pointer to this structure.
After the statement SDL_GetVideoInfo, I want to use printf() to print the
values of the structure members
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-02-27 08:26 +0000 |
| Message-ID | <mcp9lu$g35$1@dont-email.me> |
| In reply to | #57734 |
On 27/02/15 08:21, bilsch wrote:
> This is in the #include file SDL_video.h
>
> typedef struct SDL_VideoInfo {
> Uint32 hw_available :1;
> Uint32 wm_available :1;
> Uint32 UnusedBits1 :6;
> Uint32 UnusedBits2 :1;
> Uint32 blit_hw :1;
> Uint32 blit_hw_CC :1;
> Uint32 blit_hw_A :1;
> Uint32 blit_sw :1;
> Uint32 blit_sw_CC :1;
> Uint32 blit_sw_A :1;
> Uint32 blit_fill :1;
> Uint32 UnusedBits3 :16;
> Uint32 video_mem;
> SDL_PixelFormat *vfmt;
> int current_w;
> int current_h;
> } SDL_VideoInfo;
>
> The function SDL_GetVideoInfo returns a pointer to this structure.
>
> After the statement SDL_GetVideoInfo, I want to use printf() to print the
> values of the structure members
>
>
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");
}
...then you're not telling us everything.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 10:37 +0000 |
| Message-ID | <mcphds$vq$3@dont-email.me> |
| In reply to | #57736 |
>>
>>
>>
> 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.
Next check if p is something besides NULL.
If yes get 32 bits from hw_available and print as long unsigned integer.
I have never seen the notation -> before (except Mr. Schwarz post).
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-02-27 03:16 -0800 |
| Message-ID | <oqj0fadn2p5bp8gja3k1dqpn6ojtfnd82v@4ax.com> |
| In reply to | #57745 |
On Fri, 27 Feb 2015 10:37:48 +0000 (UTC), 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.
>Next check if p is something besides NULL.
>If yes get 32 bits from hw_available and print as long unsigned integer.
>
>I have never seen the notation -> before (except Mr. Schwarz post).
If you have an object of type structure, then the . operator is used
to select members. So given
SDL_VideoInfo st; /* a structure object */
then
printf("...", st.hw_available);
would print the value of the first member of the structure.
When you have an object of type pointer to structure, the -> operator
allows is used to select members as Richard posted. You do have
another option but it is more cumbersome to type and to read. Given p
as above, you can use the * operator to dereference the pointer. When
you dereference a pointer, the expression evaluates to the object
pointed to. Since p points to a structure, *p evaluates to that
structure. Thus
printf("...", (*p).hw_available);
is exactly the same as the code Richard wrote.
And just to make it interesting, (*p) is obviously equivalent to
(*p+0) which is by definition equivalent to p[0]. Thus
printf("...", p[0].hw_available);
is also the same.
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 12:52 +0000 |
| Message-ID | <mcppb5$vq$4@dont-email.me> |
| In reply to | #57747 |
Thanks. I tried all 4 ways. Very informative.
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2015-02-27 11:25 +0000 |
| Message-ID | <clb2h9F1vusU1@mid.uni-berlin.de> |
| In reply to | #57745 |
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
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2015-02-27 11:31 +0000 |
| Message-ID | <clb2t4F1vusU2@mid.uni-berlin.de> |
| In reply to | #57748 |
Jens Thoms Toerring <jt@toerring.de> wrote:
> #include <inttypes.h>
> printf("hw_available: %PRIu32"\n", p->hw_available);
Uuups, lost a quote here, it has to be
printf("hw_available: %"PRIu32"\n", p->hw_available);
i.e. this 'PRIu32' thingy is some kind of macro that gets
replaed by a string literal and the whole thing depends
on the automatic concatenation of string literals.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 22:58 +0000 |
| Message-ID | <mcqsqt$o4a$2@dont-email.me> |
| In reply to | #57750 |
Thanks for the info.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-02-27 08:20 -0800 |
| Message-ID | <87twy75nsz.fsf@kst-u.example.com> |
| In reply to | #57748 |
jt@toerring.de (Jens Thoms Toerring) writes:
[...]
> 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":
[snip]
(Pedantic quibble: arrays have elements, structures have members.)
You probably didn't notice that hw_available is actually a bit field:
Uint32 hw_available :1;
It can only have the values 0 and 1, so it's sufficient to convert to
unsigned and print using "%u":
printf("hw_available: %u\n", (unsigned) p->hw_available);
A few more comments:
Support for bit fields of types other than int, signed int, unsigned
int, and _Bool (bool) is implementation-defined; the signedness of a
plain "int" bit field is implementation-defined.
Presumably Uint32 is a typedef for an unsigned 32-bit integer type. In
modern C code, there's no good reason not to use uint32_t, defined in
<stdint.h>, for this purpose. (There are some odd technicalities for
platforms with non-2's-complement signed integers, but they're unlikely
to apply.) Perhaps Uint32 was defined for some pre-C99 implementatioon,
and kept for backward compatibility.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 22:55 +0000 |
| Message-ID | <mcqsl4$o4a$1@dont-email.me> |
| In reply to | #57753 |
Thanks for the info.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-02-26 22:03 -0800 |
| Message-ID | <i710fahl72mm5hjbobjcbgpdup4va39muv@4ax.com> |
| In reply to | #57718 |
On Fri, 27 Feb 2015 03:42:52 +0000 (UTC), bilsch <king621@comcast.net>
wrote:
>hypothetical function getstructure() returns a pointer to a structure of
>the form:
>
>struct{int one;int two;}strc1.
>
>The structure name is already included via a header file so I cant use it
How is the structure name "included"? Why? Does it have the extern
storage class specified?
>it the program. I want to do something like the following (which doesn't
>work) but I don't know how to assign names to the structure members. TIA.
You don't assign names to the structure members. The definition of
the structure type (which should be in a header) does so.
>printf("int1= %i int2= %i /n", strc1.one, strc1.two);
If function getstructure returns a pointer to the struct you show,
then it would be prudent for the definition of the structure type to
be in scope at the point the function is called. This usually means
that the declaration defining the structure type should have a tag
name which you omitted. Then you can code something like
struct mystruct *ptr = getstructure(...);
followed by
printf("int1= %i int2= %i /n", ptr->one, ptr->two);
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2015-02-26 23:06 -0800 |
| Message-ID | <db6da0f1-8ede-4b83-8c17-360c40d95e79@googlegroups.com> |
| In reply to | #57728 |
Well I'm glad we've gotten off the topic of UFOs. Finally, back to discussing C.
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2015-02-27 08:23 +0000 |
| Message-ID | <mcp9iu$vq$2@dont-email.me> |
| In reply to | #57728 |
This is in the #include file SDL_video.h
typedef struct SDL_VideoInfo {
Uint32 hw_available :1;
Uint32 wm_available :1;
Uint32 UnusedBits1 :6;
Uint32 UnusedBits2 :1;
Uint32 blit_hw :1;
Uint32 blit_hw_CC :1;
Uint32 blit_hw_A :1;
Uint32 blit_sw :1;
Uint32 blit_sw_CC :1;
Uint32 blit_sw_A :1;
Uint32 blit_fill :1;
Uint32 UnusedBits3 :16;
Uint32 video_mem;
SDL_PixelFormat *vfmt;
int current_w;
int current_h;
} SDL_VideoInfo;
The function SDL_GetVideoInfo returns a pointer to this structure.
After the statement SDL_GetVideoInfo, I want to use printf() to print the
values of the structure members
[toc] | [prev] | [next] | [standalone]
| From | Nick Bowler <nbowler@draconx.ca> |
|---|---|
| Date | 2015-03-02 18:40 +0000 |
| Message-ID | <md2aqs$16c$1@dont-email.me> |
| In reply to | #57735 |
On Fri, 27 Feb 2015 08:23:58 +0000, bilsch wrote:
> This is in the #include file SDL_video.h
>
> typedef struct SDL_VideoInfo {
> Uint32 hw_available :1;
> Uint32 wm_available :1;
> Uint32 UnusedBits1 :6;
> Uint32 UnusedBits2 :1;
> Uint32 blit_hw :1;
> Uint32 blit_hw_CC :1;
> Uint32 blit_hw_A :1;
> Uint32 blit_sw :1;
> Uint32 blit_sw_CC :1;
> Uint32 blit_sw_A :1;
> Uint32 blit_fill :1;
> Uint32 UnusedBits3 :16;
> Uint32 video_mem;
> SDL_PixelFormat *vfmt;
> int current_w;
> int current_h;
> } SDL_VideoInfo;
>
> The function SDL_GetVideoInfo returns a pointer to this structure.
>
> After the statement SDL_GetVideoInfo, I want to use printf() to print the
> values of the structure members
We don't know what Uint32 is, but if we assume it is a typedef for some
unsigned integer type that's intended to store the values between
0-4294967295 (inclusive) then we can print all these members.
The first 10 members are bit-fields. The C specification does not
define promotion for bit-fields of types other than _Bool, int,
signed, or unsigned (note that the C standard leaves all other types of
bit-fields implementation-defined). So you can try to figure out what
your compiler actually does from its documentation and/or experiment,
or you can just use a cast. "unsigned" will do for all of these, e.g.:
printf("hw_available: %u\n", (unsigned)videoinfo->hw_available);
The video_mem member is Uint32. Based on our assumption, "unsigned
long" is sufficient to print this, and we again use a cast. For
example:
printf("video_mem: %lu\n", (unsigned long)videoinfo->video_mem);
The vfmt member is a pointer to some typedef. We don't know what the
typedef is, but I assume it's a struct type. We can print that as a
void * using another cast. Something like:
printf("vfmt: %p\n", (void *)videoinfo->vfmt);
Finally, the current_w and current_h members are ints, which can be
printed directly. As in:
printf("current_w: %d\n", videoinfo->current_w);
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-03-02 14:28 -0500 |
| Message-ID | <54F4B9E1.1090603@verizon.net> |
| In reply to | #57824 |
On 03/02/2015 01:40 PM, Nick Bowler wrote: ... > ... The C specification does not > define promotion for bit-fields of types other than _Bool, int, > signed, or unsigned (note that the C standard leaves all other types of > bit-fields implementation-defined). ... "If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.58) All other types are unchanged by the integer promotions." 6.3.1.1p2 The promotions depend upon the implementation-defined values of INT_MIN and INT_MAX, and depend upon the implementation-defined range of values allowed for each integer type. For bit-fields, the range is uniquely determined by the size in bits, except with regard to the handling of the sign bit for signed bit fields. I'd expect the sign bit to be handled the same way as it is in the corresponding non-bit-field type, but it's arguable whether or not that's clearly required. However, modulo those issues, 6.3.1.1p2 uniquely determines the promoted type and the promoted value. > ... So you can try to figure out what > your compiler actually does from its documentation and/or experiment, Such experimentation is therefore needed only for determining whether the implementation is conforming.
[toc] | [prev] | [next] | [standalone]
| From | Nick Bowler <nbowler@draconx.ca> |
|---|---|
| Date | 2015-03-03 15:07 +0000 |
| Message-ID | <md4in6$16c$2@dont-email.me> |
| In reply to | #57826 |
On Mon, 02 Mar 2015 14:28:33 -0500, James Kuyper wrote:
> On 03/02/2015 01:40 PM, Nick Bowler wrote:
> ...
>> ... The C specification does not
>> define promotion for bit-fields of types other than _Bool, int,
>> signed, or unsigned (note that the C standard leaves all other types of
>> bit-fields implementation-defined). ...
>
> "If an int can represent all values of the original type (as restricted
> by the width, for a bit-field), the value is converted to an int;
> otherwise, it is converted to an unsigned int. These are called the
> integer promotions.58) All other types are unchanged by the integer
> promotions." 6.3.1.1p2
You did not quote the whole section 6.3.1.1p2. It begins with the
following restriction:
The following may be used in an expression wherever an int or unsigned
int may be used:
* An object or expression with an integer type (other than int or
unsigned int) whose integer conversion rank is less than or equal
to the rank of int and unsigned int.
* A bit-field of type _Bool, int, signed int, or unsigned int.
Note especially the second bullet point. The restriction is essential,
otherwise "long" would promote to "int" on some implementations.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web