Article View: comp.lang.c
Article #1287Re: p = {1,0,0}
From: David Brown
Date: Tue, 03 Nov 2026 10:09
Date: Tue, 03 Nov 2026 10:09
143 lines
7021 bytes
7021 bytes
On 02/11/2026 23:58, bartc wrote:
> On 02/11/2026 20:29, David Brown wrote:
>> On 02/11/2026 19:47, bartc wrote:
>>> On 02/11/2026 16:36, David Brown wrote:
>>>> On 01/11/2026 14:41, bartc wrote:
>>>>> On 01/11/2026 13:32, fir wrote:
>>>>>> it is maybe banal but sometimes banal remarks also maybe should be
>>>>>> stated as i dont remember if i even was posting on that
>>>>>>
>>>>>> the fact stat some cannot assign a structure like
>>>>>> this
>>>>>>
>>>>>> s = {x,y};
>>>>>>
>>>>>> seems rather silly (especially as structures can be assigned/copied)
>>>>>>
>>>>>> with this {0,0,1} at least c has a basic of ths vector/tupple support
>>>>>> and this 'native' support is quite standable..so its very weird it
>>>>>> is 'blocked' from usage
>>>>>
>>>>>
>>>>> #include <stdio.h>
>>>>>
>>>>> typedef struct {int x,y;} T;
>>>>>
>>>>> int main(void) {
>>>>> T s;
>>>>> s = (T){10,20};
>>>>> }
>>>>>
>>>>
>>>> As often seems to be the case, things Fir wants are already
>>>> available in C++.
>>>>
>>>> In C, you need to give a type to "{ 10, 20 }" - as you did here,
>>>> making it into a compound literal. The expression "{ 10, 20 }" on
>>>> its own cannot be used for assignment to "s" because the type of an
>>>> expression must be determined from the expression alone - not from
>>>> what you are assigning it to.
>>>
>>> I think this has been discussed recently but I can't remember the
>>> arguments.
>>>
>>> But people can write:
>>>
>>> x = 0;
>>>
>>> and the exact type of '0' can be determined from the type of 'x', so
>>> I don't see much difference.
>>
>> No, 0 is of type "int". As an integer constant expression with value
>> 0, it can be /converted/ to any pointer type, giving a null pointer
>> constant. It can also be converted to a float, or other arithmetic
>> type. But as it stands, "0" has a fixed type - "int".
>
> Same thing. Here there is a tuple of two ints. (The syntax might be
> problematical in C outside of initialisation or following a cast.)
In C, it is not a "tuple of two ints". C has no concept of "tuple".
This is simply the syntax used for multiple initialisers. That is where
the limitation lies - there is no way in C to have a kind of on-the-fly
structure or array type. (Some languages have it, C does not.) When
you write "(T) { 10, 20 }", you are /not/ writing a cast or any other
kind of type conversion. You are using the particular syntax for a
"compound literal". The syntax is obviously picked to be similar to a
cast, and similar to an initialisation, but it is /not/ a cast. It is
creating a temporary object of type T that is initialised with the
values 10 and 20.
>
> Such tuples can be used without having to expose them in the language.
> They already occur here: (T){10, 20}.
>
>
>
>
>>>
>>> Plus it doesn't see to be a problem here:
>>>
>>> T s = {10, 20};
>>>
>>> What determines the type of {10, 20} in this case?
>>>
>>
>> That is initialisation, which is a different matter. Critically, it
>> is a declaration and not an expression - it does not produce a value
>> that can itself be used.
>
> But it manages to figure out the types. (Even if it is not fussy about
> nested {} grouping.)
The types relevant to initialisation come from the initialised type.
When you write "T s = { 10, 20 };", you do not have an object with value
"{ 10, 20 }" of some automatic type that is then assigned to "s". What
you have is a short-cut syntax for :
T s;
s.x = 10;
s.y = 20;
"{ 10, 20 }" does not exist on its own, it does not have a type, it is
not a "tuple", there are no casts or conversions, and it is not assigned
to "s" or anything else.
>
>
>> (I'd be happy if assignment were a particular kind of statement,
>> rather than an expression. I'd be happy if "s = { 10, 20 };" worked
>> in C. It doesn't work in C, however - this is one of many reasons why
>> I usually work in C++ rather than C.)
>
> OK. Another reason why such things are not a priority in C. You either
> use workarounds with macros or switch to a sister language which has
> evolved much more dramatically.
That paragraph would make sense if I were the benevolent dictator for
life for the C programming language. I am not.
C++ has many features that C does not. It also has many complications.
These are tradeoffs, and for much of the work I do, C++ is usually a
better choice for me. (For other things I do, Python is usually a
better choice than either C or C++. And for some things, C is a better
choice.) It is undoubtedly the case that C++ has seen many more
dramatic changes than C. It is also undoubtedly the case that much of
the complication of C++ as a language is because of features being
retrofitted into a language that did not support them originally.
>
>>>>
>>>> In C++, "s = { 10, 20 };" is valid because "{ 10, 20 }" is treated
>>>> as type std::initializer_list<int> (the "int" part is automatically
>>>> inferred). Making this work involves quite a bit of additional C++
>>>> machinery that does not exist in C.
>>> In the language or compiler? Because machinery can be added to C too.
>>>
>>
>> std::initializer_list is a template. Making templates work in C would
>> require so many changes that it would be pointless - just use C++.
>>
>> Maybe the assignment could be made to work in other ways that could be
>> more feasible to add to C. It's not something I have thought through
>> in detail.
>
Technical Details
Message-ID:
<11ccbsi$3l776$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11c7f1j$20b9h$1@dont-email.me> <11c7fj6$20gab$1@dont-email.me> <11cae5m$318tq$1@dont-email.me> <11calrt$34gq3$1@dont-email.me> <11carqs$36m0o$1@dont-email.me> <11cb4hs$3a13l$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: p = {1,0,0}
Date: Tue, 3 Nov 2026 11:09:21 +0100
Organization: A noiseless patient Spider
Lines: 143
Message-ID: <11ccbsi$3l776$1@dont-email.me>
References: <11c7f1j$20b9h$1@dont-email.me> <11c7fj6$20gab$1@dont-email.me>
<11cae5m$318tq$1@dont-email.me> <11calrt$34gq3$1@dont-email.me>
<11carqs$36m0o$1@dont-email.me> <11cb4hs$3a13l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 03 Nov 2026 10:09:23 +0000 (UTC)
Injection-Info: dont-email.me;
logging-data="3841254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kw8aqYRubIfZE21b2jWhz9frIvtNlbRI="; posting-host="f06b12b7403cf57442a23e3e44a23173"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lvSEhbrjOKk/fXOxjteztaZ/p3w= sha256:kAhGL42xbCmft1JvOF6xZyShg7CdzjKh2cnIhehiPjA=
sha1:1+HBbX60iKSbNml4MgSe6iF78Zw= sha256:zJQ+cqR4arlmFPDNke0z9DXW6zcQ4yOJdzp4IiyxZEQ=
Content-Language: en-GB, nb-NO
In-Reply-To: <11cb4hs$3a13l$1@dont-email.me>