Possible occurrences of "char [3]"

J

John Reye

Hi there,

where could the following legally occurr in C code - not counting in
strings ;)

char [3]


I can think only of:
sizeof(char [3]);
Are there any other possibilities?

Thanks.
 
J

John Reye

In particular:
It is never possible to use that is a cast, right?


(char [3]) var /* this cast is never allowed, right? */
 
B

Ben Bacarisse

John Reye said:
where could the following legally occurr in C code - not counting in
strings ;)

and character literals and comments and macro bodies, etc...
char [3]

I can think only of:
sizeof(char [3]);
Are there any other possibilities?

also in compound literals: (char [3]){"ab"}
 
J

Jens Gustedt

Am 05/03/2012 11:03 PM, schrieb John Reye:
Hi there,

where could the following legally occurr in C code - not counting in
strings ;)

char [3]


I can think only of:
sizeof(char [3]);
Are there any other possibilities?

_Alignof(char[3])
_Alignas(char[3])

and their counteparts with macros alignof and alignas

Jens
 
J

John Reye

In particular:
It is never possible to use that is a cast, right?

(char [3]) var    /* this cast is never allowed, right? */

I do actually believe that a cast is possible:
Example
printf("%s\n", (char [3]) {'h', 'i', '\0'} );
 
B

Ben Bacarisse

John Reye said:
In particular:
It is never possible to use that is a cast, right?

(char [3]) var    /* this cast is never allowed, right? */

I do actually believe that a cast is possible:
Example
printf("%s\n", (char [3]) {'h', 'i', '\0'} );

That's not a cast. It's a compound literal.

(Anyone know why that are called that since they need not be compound
and they don't seem to be literals -- at least not like any other C
literals?)
 
J

Jens Gustedt

Am 15.05.2012 04:19, schrieb Ben Bacarisse:
That's not a cast. It's a compound literal.

(Anyone know why that are called that since they need not be compound
and they don't seem to be literals -- at least not like any other C
literals?)

It's a misnomer (like others) a failed attempt to introduce "real"
literals for all types of classes. "Real" literals would be given by
const qualified register variables in file scope (which are
unfortunately not allowed.)

My guess is that the analogy is not taken from literals for arithmetic
types but from "string literals", which aren't "literals" either, but
unmutable objects.

Jens
 
B

Ben Bacarisse

Jens Gustedt said:
Am 15.05.2012 04:19, schrieb Ben Bacarisse:


It's a misnomer (like others) a failed attempt to introduce "real"
literals for all types of classes. "Real" literals would be given by
const qualified register variables in file scope (which are
unfortunately not allowed.)

My guess is that the analogy is not taken from literals for arithmetic
types but from "string literals", which aren't "literals" either, but
unmutable objects.

Yes, there is a similarity there in that both make objects, but string
literals are (as you say) immutable which is, in my mind, a key property
of a literal. Compound literals are modifiable lvalues. I'd rather
they were called "anonymous objects" but that boat has sailed.
 
J

Jens Gustedt

Am 15.05.2012 22:28, schrieb Ben Bacarisse:
Compound literals are modifiable lvalues. I'd rather
they were called "anonymous objects"

that would be ambiguous, too, since malloced objects are the same.

anonymous automatic variables?
but that boat has sailed.

yes, unfortunately

Jens
 
B

Ben Bacarisse

Jens Gustedt said:
Am 15.05.2012 22:28, schrieb Ben Bacarisse:

that would be ambiguous, too, since malloced objects are the same.
True.

anonymous automatic variables?


yes, unfortunately

I'll resist the urge to make any other suggestions since it's done and
dusted.
 
T

Tim Rentsch

Ben Bacarisse said:
John Reye said:
In particular:
It is never possible to use that is a cast, right?

(char [3]) var /* this cast is never allowed, right? */

I do actually believe that a cast is possible:
Example
printf("%s\n", (char [3]) {'h', 'i', '\0'} );

That's not a cast. It's a compound literal.

(Anyone know why that are called that since they need not be compound
and they don't seem to be literals -- at least not like any other C
literals?)

The term makes sense to me. Typically they are used for
"compound" data types (a struct, union, or array); certainly
their most obvious purpose is for "compound" values even if
"ordinary" values are also allowed. As for literal, historically
a "literal" was a memory location (or several) referred to by
giving its contents locally rather than defining it elsewhere and
referring to it using a label. That a 'literal' is an lvalue
rather than an rvalue may seem odd in C but it's consistent with
other uses of the term that predate C (I remember it from various
ancient assembly languages -- IIRC these things were usually
called "literals" but they were just a quick way to refer to a
regular memory location).
 
T

Tim Rentsch

Jens Gustedt said:
Am 15.05.2012 22:28, schrieb Ben Bacarisse:

that would be ambiguous, too, since malloced objects are the same.

anonymous automatic variables?

Except compound literals need not be automatic; they
also are allowed in top-level initializers.

Personally I think "compound literals" is a good choice.
 

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

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,700
Latest member
jody1922

Latest Threads

Top