Compound literals (anonymous aggregates)

K

kid joe

Hi group,

I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.

Cheers,
Joe


--
.---. .-----------
/ \ __ / ------
/ / \( )/ -----
////// ' \/ ` --- /=================\
//// / // : : --- | |
// / / /` '-- | Good Evening... |
// //..\\ | |
=============UU====UU============\================/
'//||\\`
''``
 
K

Keith Thompson

kid joe said:
I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.
[16 lines deleted]

I'll be glad to, as soon as you shrink your signature to an acceptable
length. The usual guideline is no more than 4 lines. (ASCII art that
fits in 4 lines by, say, 72 columns is perfectly acceptable.)
 
B

Ben Pfaff

kid joe said:
I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.

The GCC manual actually has a pretty good explanation of the
"what" of compound literals. I don't have a good "why"
explanation though.

Here is part of what the GCC manual says:

5.20 Compound Literals
======================

ISO C99 supports compound literals. A compound literal looks like a
cast containing an initializer. Its value is an object of the type
specified in the cast, containing the elements specified in the
initializer; it is an lvalue. As an extension, GCC supports compound
literals in C89 mode and in C++.

Usually, the specified type is a structure. Assume that `struct foo'
and `structure' are declared as shown:

struct foo {int a; char b[2];} structure;

Here is an example of constructing a `struct foo' with a compound
literal:

structure = ((struct foo) {x + y, 'a', 0});

This is equivalent to writing the following:

{
struct foo temp = {x + y, 'a', 0};
structure = temp;
}

You can also construct an array. If all the elements of the compound
literal are (made up of) simple constant expressions, suitable for use
in initializers of objects of static storage duration, then the compound
literal can be coerced to a pointer to its first element and used in
such an initializer, as shown here:

char **foo = (char *[]) { "x", "y", "z" };

Compound literals for scalar types and union types are is also
allowed, but then the compound literal is equivalent to a cast.
 
K

Kenny McCormack

kid joe said:
I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.
[16 lines deleted]

I'll be glad to, as soon as you shrink your signature to an acceptable
length. The usual guideline is no more than 4 lines. (ASCII art that
fits in 4 lines by, say, 72 columns is perfectly acceptable.)

Believe me, Joe, you don't want or need any help from Keithy.

Keep the sig - I really like it - and it wards off the useless posters
(like Keithy).

Now, we will see if Keithy will keep his promise and ignore you (that
is, contribute no more noise to the newsgroup on this subject) until you
change your sig (or when pigs fly, whichever comes first).

It'd be really nice if Keithy would keep his promises to ignore
everything in this newsgroup which annoys him (which is just about
everything) - in which case he'd be contributing no more noise, period.
 
K

kid joe

I'll be glad to, as soon as you shrink your signature to an acceptable
length. The usual guideline is no more than 4 lines. (ASCII art that
fits in 4 lines by, say, 72 columns is perfectly acceptable.)

Hi Keith,

I used to have a much bigger signature, with an ASCII art self-portrait
giving a friendly greeting. I changed to the smaller one, I think at your
request. I estimate that my current signature is approximately 6 lines. If
it's still too big, I'll try to come up with another one, but it may take
a little while.

It's a pity people often don't like ASCII art, I think it's pretty cool.

Cheers,
Joe


--

.---. .-----------
/ \ __ / ------
/ / \( )/ -----
////// ' \/ ` --- /=================\
//// / // : : --- | |
// / / /` '-- | Good Evening... |
// //..\\ | |
=============UU====UU============\================/
'//||\\`
''``
 
K

Keith Thompson

kid joe said:
I used to have a much bigger signature, with an ASCII art self-portrait
giving a friendly greeting. I changed to the smaller one, I think at your
request. I estimate that my current signature is approximately 6 lines. If
it's still too big, I'll try to come up with another one, but it may take
a little while.

It's 10 lines.

Think of it as an artistic challenge.
It's a pity people often don't like ASCII art, I think it's pretty cool.

I didn't say I dislike it.
 
K

kid joe

It's 10 lines.

Think of it as an artistic challenge.

Hi Keith,

It's really difficult to fit any detail into 4 lines! Try it...

Anyway I came up with two possibilities - if anyone has any opinions let
me know.

Cheers,
Joe


Still very slightly too big:

''~``
( o o )
+------------------.oooO--(_)--Oooo.------------------+
| |
| G O O D .oooO E V E N I N G ! |
| ( ) Oooo. |
+---------------------\ (----( )--------------------+
\_) ) /
(_/



Literally 4 lines:


...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|
 
K

Keith Thompson

kid joe said:
Hi Keith,

It's really difficult to fit any detail into 4 lines! Try it...

Anyway I came up with two possibilities - if anyone has any opinions let
me know.


Still very slightly too big:
[9 lines deleted]

9 lines is "very slightly too big"?
Literally 4 lines:


...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|

I certainly have no problem with that. I would have no problem with
just about anything that fits in 4 lines. It's yours to do with as
you like.
 
D

Default User

Richard said:
Keith Thompson said:

Actually, I rather like it.


I think kid joe's bird-art makes comp.lang.c a very slightly
brighter place, and I welcome it.

I'll point out that the item in question does not have a correct
separator. It's missing the trailing space.




Brian
 
B

BartC

It's really difficult to fit any detail into 4 lines! Try it...

Anyway I came up with two possibilities - if anyone has any opinions let
me know.
Still very slightly too big:

How about posting a link (in the sig) to the Ascii Art, for those who are
interested? Then it can be as impressive as you like.

BTW they are wasted on those using proportional fonts (like me).
 
L

luserXtrog

Hi group,

I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
    ( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.

It allows you to create literal structures and arrays using
a syntax borrowed from declarations where you'd otherwise have
to create a variable.

I came up with this recently for a dude who wanted the conditional
operator to work like a switch statement.


#include <stdio.h>

int Init(int i) { puts( "Init"); return i+1;}
int Work(int i) { puts( "Work"); return i+1;}
int Draw(int i) { puts( "Draw"); return i+1;}
int Next(int i) { puts( "Next"); return i+1;}
int CleanUp(int i) { puts("CleanUp"); return -1;}

int main(void) {
int A = 0; /*while queueb noop loop*/
while((A=A[(int(*[])(int)){Init,Work,Draw,Next,CleanUp}](A))!=-1)
0xE0L;
return 0;
}

/*eof*/

(De)obfuscation notes:
# array[index] is equivalent to index[array]
# 0xE0L is a void expression. It doesn't do anything.

hth
 
I

Ian Collins

Keith said:
kid joe said:
I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
( type-name ) { initializer-list }

Can anyone explain what the point of this is and give an example of how
it's used.
[16 lines deleted]

I'll be glad to, as soon as you shrink your signature to an acceptable
length. The usual guideline is no more than 4 lines. (ASCII art that
fits in 4 lines by, say, 72 columns is perfectly acceptable.)
It wouldn't be so bad if he got the time of day right....
 
G

Guest

I was reading a page on the new features in C99 and I saw that compound
literals (anonymous aggregates) can be created using the notation
    ( type-name ) { initializer-list }
Can anyone explain what the point of this is and give an example of how
it's used.
[16 lines deleted]
I'll be glad to, as soon as you shrink your signature to an acceptable
length.  The usual guideline is no more than 4 lines.  (ASCII art that
fits in 4 lines by, say, 72 columns is perfectly acceptable.)

Believe me, Joe, you don't want or need any help from Keithy.

Keith sometimes posts useful and helpful advice. Kenny hardly ever
(check this). You decide which one you want to reply to your posts.

<snip>
 
G

Guest

<snip>

I have in the past argued against long sigs for the
sake of people who don't have broadband, but I suspect that this
number is becoming vanishingly low. Practically everybody has
broadband. The last time I personally observed someone actually
using a modem must have been four or five years ago.

I know two people who still use modems. Though they don't
post to usenet.

<snip>
 
B

Ben Bacarisse

<snip>

I have in the past argued against long sigs for the

I know two people who still use modems. Though they don't
post to usenet.

It is not just about bandwidth. I read by tapping one key. Longs
sigs and bad quoting mean I need page though more noise to read the
signal. It make almost no difference if one person has a long sig or
a couple of infrequent posters don't snip well, but I really feel the
difference when reading other groups. I'd rather c.l.c did not go
that way.
 
C

CBFalconer

Keith said:
.... snip ...

9 lines is "very slightly too big"?
Literally 4 lines:

...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|

I certainly have no problem with that. I would have no problem
with just about anything that fits in 4 lines. It's yours to do
with as you like.

Well done. I guess I can unplonk him.
 
P

Phil Carmody

CBFalconer said:
I do. Quite enough for usenet.

I'm using the same modem I used in the 90s - a Nokia BB2M. I
don't use "dial-up", and I don't use "POTS", but I certainly
use a "modem". I found symmetric 2MB/s quite sufficient back
then, and my usage habits haven't changed much, so it's quite
sufficient now.

Phil
 
F

Flash Gordon

Phil said:
I'm using the same modem I used in the 90s - a Nokia BB2M. I
don't use "dial-up", and I don't use "POTS", but I certainly
use a "modem". I found symmetric 2MB/s quite sufficient back
then, and my usage habits haven't changed much, so it's quite
sufficient now.

I don't use a modem and have a faster connection. However if I use too
much data I will get charged a hefty amount. I had to move to such a
connection of switch to something that would not meet my speed requirements.
 
L

lawrence.jones

Golden California Girls said:
Despite Nokia calling it a modem, it isn't. Modem MOdulator / DEModulator only
can exist if one side is an analog signal.

This is going awfully far afield, comp.dcom.telecom would be a more
appropriate forum, but the telco side of HDSL *is* analog, despite the
"digital" in the name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top