linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* C Question
@ 2010-05-25 10:08 Randi Botse
  2010-05-25 10:19 ` Xiaotian Feng
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Randi Botse @ 2010-05-25 10:08 UTC (permalink / raw)
  To: linux-c-programming

Hi All,

Im looking at lscpu.c shipped by util-linux-ng-2.17, can you explain
me how the *vir_types[] declared, is this valid C declaration?

/* virtualization types */
enum {
	VIRT_NONE	= 0,
	VIRT_PARA,
	VIRT_FULL
};
const char *virt_types[] = {
	[VIRT_NONE]	= N_("none"),
	[VIRT_PARA]	= N_("para"),
	[VIRT_FULL]	= N_("full")
};

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-25 10:08 C Question Randi Botse
@ 2010-05-25 10:19 ` Xiaotian Feng
       [not found]   ` <AANLkTinTu94E7guvzjySpGjht2Hjw4aop4vRIVpHo9UL@mail.gmail.com>
  2010-05-25 11:18 ` Uriel Corfa
  2010-05-25 11:41 ` Michal Nazarewicz
  2 siblings, 1 reply; 9+ messages in thread
From: Xiaotian Feng @ 2010-05-25 10:19 UTC (permalink / raw)
  To: Randi Botse; +Cc: linux-c-programming

On Tue, May 25, 2010 at 6:08 PM, Randi Botse <nightdecoder@gmail.com> wrote:
> Hi All,
>
> Im looking at lscpu.c shipped by util-linux-ng-2.17, can you explain
> me how the *vir_types[] declared, is this valid C declaration?
>

N_(blah) must be defined in some include files

> /* virtualization types */
> enum {
>        VIRT_NONE       = 0,
>        VIRT_PARA,
>        VIRT_FULL
> };
> const char *virt_types[] = {
>        [VIRT_NONE]     = N_("none"),
>        [VIRT_PARA]     = N_("para"),
>        [VIRT_FULL]     = N_("full")
> };
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
       [not found]   ` <AANLkTinTu94E7guvzjySpGjht2Hjw4aop4vRIVpHo9UL@mail.gmail.com>
@ 2010-05-25 10:49     ` Randi Botse
  0 siblings, 0 replies; 9+ messages in thread
From: Randi Botse @ 2010-05-25 10:49 UTC (permalink / raw)
  To: Guillaume Leconte; +Cc: linux-c-programming

Yes, that's i was talking about, i got the idea, gcc even let me
declared "unordered" subobject index like this..

const char *my_strings[] = {
            [0] = "string1",
            [2] = "string2",
            [1] = "string3",
            etc...
}

Actually, this is my first time seeing codes like this, it was strange
for me :p, many thanks for let me know!

- Randi,


On Tue, May 25, 2010 at 5:24 PM, Guillaume Leconte
<guillaume.leconte@gmail.com> wrote:

> I think he is talking about the [VIRT_NONE] = ... style, which is C99.
>
>>
>> > /* virtualization types */
>> > enum {
>> >        VIRT_NONE       = 0,
>> >        VIRT_PARA,
>> >        VIRT_FULL
>> > };
>> > const char *virt_types[] = {
>> >        [VIRT_NONE]     = N_("none"),
>> >        [VIRT_PARA]     = N_("para"),
>> >        [VIRT_FULL]     = N_("full")
>> > };
>
> It is equivalent to:
>
> const char *virt_types[3];
>
> virt_types[VIRT_NONE] = N_("none");
> virt_types[VIRT_PARA] = N_("para");
> virt_types[VIRT_FULL] = N_("full");
>
>
>>
>> > --
>> > To unsubscribe from this list: send the line "unsubscribe
>> > linux-c-programming" in
>> > the body of a message to majordomo@vger.kernel.org
>> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
>> >
>> --
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
>
> --
> « I've seen things you people wouldn't believe.  Attack ships on fire
> off the shoulder of Orion.  I watched C-beams glitter in the dark near
> the Tanhauser gate.  All those moments will be lost in time like tears
> in rain.  Time to die. »
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-25 10:08 C Question Randi Botse
  2010-05-25 10:19 ` Xiaotian Feng
@ 2010-05-25 11:18 ` Uriel Corfa
  2010-05-25 11:41 ` Michal Nazarewicz
  2 siblings, 0 replies; 9+ messages in thread
From: Uriel Corfa @ 2010-05-25 11:18 UTC (permalink / raw)
  To: Randi Botse; +Cc: linux-c-programming

That's "designated initializers". It's C99.

It allows you to initialize some members to a specific value without
specifying the default value manually for the other ones.

Refs :
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf  chapter 6.7.8.6
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/designators.htm

On Tue, May 25, 2010 at 12:08 PM, Randi Botse <nightdecoder@gmail.com> wrote:
> Hi All,
>
> Im looking at lscpu.c shipped by util-linux-ng-2.17, can you explain
> me how the *vir_types[] declared, is this valid C declaration?
>
> /* virtualization types */
> enum {
>        VIRT_NONE       = 0,
>        VIRT_PARA,
>        VIRT_FULL
> };
> const char *virt_types[] = {
>        [VIRT_NONE]     = N_("none"),
>        [VIRT_PARA]     = N_("para"),
>        [VIRT_FULL]     = N_("full")
> };
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>



-- 
Uriel Corfa
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-25 10:08 C Question Randi Botse
  2010-05-25 10:19 ` Xiaotian Feng
  2010-05-25 11:18 ` Uriel Corfa
@ 2010-05-25 11:41 ` Michal Nazarewicz
  2010-05-30 10:41   ` Randi Botse
  2 siblings, 1 reply; 9+ messages in thread
From: Michal Nazarewicz @ 2010-05-25 11:41 UTC (permalink / raw)
  To: Randi Botse; +Cc: linux-c-programming

Randi Botse <nightdecoder@gmail.com> writes:

> Hi All,
>
> Im looking at lscpu.c shipped by util-linux-ng-2.17, can you explain
> me how the *vir_types[] declared, is this valid C declaration?
>
> /* virtualization types */
> enum {
> 	VIRT_NONE	= 0,
> 	VIRT_PARA,
> 	VIRT_FULL
> };
> const char *virt_types[] = {
> 	[VIRT_NONE]	= N_("none"),
> 	[VIRT_PARA]	= N_("para"),
> 	[VIRT_FULL]	= N_("full")
> };

As Xiaotian Feng said, N_(x) must be defined somewhere and additionally
to this this definition uses a new syntax where you can specify indexes
of elements you want to set value to, ie. the above is the same as:

const char *virt_types[] = {
	[0]	= N_("none"),
	[1]	= N_("para"),
	[2]	= N_("full")
};

which in turn is the same as:

const char *virt_types[] = {
	N_("none"),
	N_("para"),
	N_("full")
};

The advantage of the new syntax is that you can specify values in any
order and omit some.

-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-25 11:41 ` Michal Nazarewicz
@ 2010-05-30 10:41   ` Randi Botse
  2010-05-30 12:38     ` Michal Nazarewicz
  0 siblings, 1 reply; 9+ messages in thread
From: Randi Botse @ 2010-05-30 10:41 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-c-programming

Yes, the N_(x) macro used for localization that i discard in my question.

Sorry but, i just having another question, consider this code:

const char *my_strings[] = {
    [0] = "index0",
    [4] = "index4",
    [6] = "index6"
};

How many pointer to char that my_string array holds now? is that 3?
where are index [1], [2], [3], and [5] ?

2010/5/25 Michal Nazarewicz <mina86@tlen.pl>:
>
> As Xiaotian Feng said, N_(x) must be defined somewhere and additionally
> to this this definition uses a new syntax where you can specify indexes
> of elements you want to set value to, ie. the above is the same as:
>
> const char *virt_types[] = {
>        [0]     = N_("none"),
>        [1]     = N_("para"),
>        [2]     = N_("full")
> };
>
> which in turn is the same as:
>
> const char *virt_types[] = {
>        N_("none"),
>        N_("para"),
>        N_("full")
> };
>
> The advantage of the new syntax is that you can specify values in any
> order and omit some.
>
> --
> Best regards,                                         _     _
>  .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
>  ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
>  ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-30 10:41   ` Randi Botse
@ 2010-05-30 12:38     ` Michal Nazarewicz
  2010-06-01 11:59       ` Randi Botse
  0 siblings, 1 reply; 9+ messages in thread
From: Michal Nazarewicz @ 2010-05-30 12:38 UTC (permalink / raw)
  To: Randi Botse; +Cc: linux-c-programming

Randi Botse <nightdecoder@gmail.com> writes:
> Yes, the N_(x) macro used for localization that i discard in my question.
>
> Sorry but, i just having another question, consider this code:
>
> const char *my_strings[] = {
>     [0] = "index0",
>     [4] = "index4",
>     [6] = "index6"
> };

NULLs will be written at indexes 1, 2, 3 and 5.  There will be total of
7 pointers (the last one will point to "index6").

-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-05-30 12:38     ` Michal Nazarewicz
@ 2010-06-01 11:59       ` Randi Botse
  2010-06-01 13:18         ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: Randi Botse @ 2010-06-01 11:59 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-c-programming

That's clear now, i was though the 'unindexed' strings (in this case
index 1, 2, 3, and 5) remains unintialized (not initialized to NULL).

Thanks very much.

Randi,

2010/5/30 Michal Nazarewicz <mina86@tlen.pl>:
> Randi Botse <nightdecoder@gmail.com> writes:
>> Yes, the N_(x) macro used for localization that i discard in my question.
>>
>> Sorry but, i just having another question, consider this code:
>>
>> const char *my_strings[] = {
>>     [0] = "index0",
>>     [4] = "index4",
>>     [6] = "index6"
>> };
>
> NULLs will be written at indexes 1, 2, 3 and 5.  There will be total of
> 7 pointers (the last one will point to "index6").
>
> --
> Best regards,                                         _     _
>  .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
>  ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
>  ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: C Question
  2010-06-01 11:59       ` Randi Botse
@ 2010-06-01 13:18         ` Glynn Clements
  0 siblings, 0 replies; 9+ messages in thread
From: Glynn Clements @ 2010-06-01 13:18 UTC (permalink / raw)
  To: Randi Botse; +Cc: Michal Nazarewicz, linux-c-programming


Randi Botse wrote:

> >> Sorry but, i just having another question, consider this code:
> >>
> >> const char *my_strings[] = {
> >>     [0] = "index0",
> >>     [4] = "index4",
> >>     [6] = "index6"
> >> };
> >
> > NULLs will be written at indexes 1, 2, 3 and 5.  There will be total of
> > 7 pointers (the last one will point to "index6").
> 
> That's clear now, i was though the 'unindexed' strings (in this case
> index 1, 2, 3, and 5) remains unintialized (not initialized to NULL).

If you specify an initialiser, the entire object is initialised. If
you specify an incomplete initialiser, the unspecified elements are
initialised to the appropriate zero value (0, 0.0, '\0', NULL, etc).

The C99 standard says (6.7.8p21):

       [#21]  If  there  are fewer initializers in a brace-enclosed
       list than there are elements or members of an aggregate,  or
       fewer  characters  in a string literal used to initialize an
       array of known size than there are elements  in  the  array,
       the   remainder   of  the  aggregate  shall  be  initialized
       implicitly the same as  objects  that  have  static  storage
       duration.

This applies to both traditional C89 sequential initialisers and C99
designated initialisers. It allows you to initialise large arrays or
structures without having to specify every element; e.g.:

	int array[1000] = {0};
	struct foo f = {0};

will initialise the entire array or structure to zero.

-- 
Glynn Clements <glynn@gclements.plus.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-06-01 13:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-25 10:08 C Question Randi Botse
2010-05-25 10:19 ` Xiaotian Feng
     [not found]   ` <AANLkTinTu94E7guvzjySpGjht2Hjw4aop4vRIVpHo9UL@mail.gmail.com>
2010-05-25 10:49     ` Randi Botse
2010-05-25 11:18 ` Uriel Corfa
2010-05-25 11:41 ` Michal Nazarewicz
2010-05-30 10:41   ` Randi Botse
2010-05-30 12:38     ` Michal Nazarewicz
2010-06-01 11:59       ` Randi Botse
2010-06-01 13:18         ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).