Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Per buck you get more computing action with the small computer. -- R. W. Hamming


devel / comp.lang.c / Initializing a char array with conditional expr

SubjectAuthor
* Initializing a char array with conditional exprSteve Keller
+- Re: Initializing a char array with conditional exprBen Bacarisse
+* Re: Initializing a char array with conditional exprbart c
|+- Re: Initializing a char array with conditional exprBen Bacarisse
|`* Initializing a char array with conditional exprSteve Keller
| +* Re: Initializing a char array with conditional exprLew Pitcher
| |`- Re: Initializing a char array with conditional exprSteve Keller
| `* Re: Initializing a char array with conditional exprDavid Brown
|  `* Re: Initializing a char array with conditional exprSteve Keller
|   `- Re: Initializing a char array with conditional exprDavid Brown
`- Re: Initializing a char array with conditional exprAndrey Tarasevich

1
Initializing a char array with conditional expr

<tdsccf$dbt$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22567&group=comp.lang.c#22567

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Lw2+UPbXfHTW4dX5M+AlEw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Initializing a char array with conditional expr
Date: Sun, 21 Aug 2022 06:30:04 +0200
Organization: Aioe.org NNTP Server
Message-ID: <tdsccf$dbt$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="13693"; posting-host="Lw2+UPbXfHTW4dX5M+AlEw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Sun, 21 Aug 2022 04:30 UTC

I'd like to initialize a char array with a conditional string constant
where the condition can be computed at compile time. Although this
works for int and char pointer, it does not work with char array.
Both, GCC and Clang give error messages for the 3. initialization in
the following code:

$ cat foo.c
#define COND() (1 < 2)
int i = COND() ? 42 : 4711;
char *p = COND() ? "42" : "4711";
char s[8] = COND() ? "42" : "4711";
$ gcc -S foo.c
foo.c:1:16: error: invalid initializer
1 | #define COND() (1 < 2)
| ^
foo.c:4:13: note: in expansion of macro ���COND���
4 | char s[8] = COND() ? "42" : "4711";
| ^~~~
$ clang -S foo.c
foo.c:4:6: error: array initializer must be an initializer list or string literal
char s[8] = COND() ? "42" : "4711";
^
1 error generated.

Why can't a char array have an expression as initializer?

Steve

Re: Initializing a char array with conditional expr

<874jy526qg.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22569&group=comp.lang.c#22569

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Sun, 21 Aug 2022 12:09:27 +0100
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <874jy526qg.fsf@bsb.me.uk>
References: <tdsccf$dbt$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="a836a4cae643b17f016adf7dc2f2ba63";
logging-data="2454728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WPq4g+l6wEpCcRtlfTDaK2UDPE8ejSVo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:kPE4AvsS5JCBSjuiqjLNc7dyf58=
sha1:ueCoq+C23O5ARejPjbbw/d664CE=
X-BSB-Auth: 1.fef02974c2933c6bc0c7.20220821120927BST.874jy526qg.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 21 Aug 2022 11:09 UTC

Steve Keller <keller.steve@gmx.de> writes:

> I'd like to initialize a char array with a conditional string constant
> where the condition can be computed at compile time. Although this
> works for int and char pointer, it does not work with char array.
> Both, GCC and Clang give error messages for the 3. initialization in
> the following code:
>
> $ cat foo.c
> #define COND() (1 < 2)
> int i = COND() ? 42 : 4711;
> char *p = COND() ? "42" : "4711";
> char s[8] = COND() ? "42" : "4711";
> $ gcc -S foo.c
> foo.c:1:16: error: invalid initializer
> 1 | #define COND() (1 < 2)
> | ^
> foo.c:4:13: note: in expansion of macro ‘COND’
> 4 | char s[8] = COND() ? "42" : "4711";
> | ^~~~
> $ clang -S foo.c
> foo.c:4:6: error: array initializer must be an initializer list or string literal
> char s[8] = COND() ? "42" : "4711";
> ^
> 1 error generated.
>
> Why can't a char array have an expression as initializer?

It depends on what sort of answer you want! "The language does not
permit it" is one possible answer, and the second error message gives a
slightly more detailed version of the same: C's initialisers are rather
limited.

But there are other factors in play here. Even if the syntax were
permitted, the string literal would not survive the conditional
expression. Writing

char a[] = "abc";

is a special case where the array "abc" is /not/ converted to a pointer
to its first element. An expression like c ? "a" : "b" /never/ has an
array value; its always of type char *.

Bottom line... you just have to accept that you can't do that in C.

PS: Your quoted error message had a bad case of mojibake. I've tried to
fix it.

--
Ben.

Re: Initializing a char array with conditional expr

<e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22570&group=comp.lang.c#22570

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:8606:0:b0:6b8:e6d7:af09 with SMTP id i6-20020a378606000000b006b8e6d7af09mr10593926qkd.416.1661095525509;
Sun, 21 Aug 2022 08:25:25 -0700 (PDT)
X-Received: by 2002:a05:6808:f09:b0:343:9876:5cae with SMTP id
m9-20020a0568080f0900b0034398765caemr9654685oiw.115.1661095525220; Sun, 21
Aug 2022 08:25:25 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 21 Aug 2022 08:25:25 -0700 (PDT)
In-Reply-To: <tdsccf$dbt$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=94.175.38.125; posting-account=rqC7UgoAAACeVvrGykivrxfPIl3bA_1y
NNTP-Posting-Host: 94.175.38.125
References: <tdsccf$dbt$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
Subject: Re: Initializing a char array with conditional expr
From: bart4...@gmail.com (bart c)
Injection-Date: Sun, 21 Aug 2022 15:25:25 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1812
 by: bart c - Sun, 21 Aug 2022 15:25 UTC

On Sunday, 21 August 2022 at 05:30:24 UTC+1, Steve Keller wrote:

> char s[8] = COND() ? "42" : "4711";

> Why can't a char array have an expression as initializer?

It needs to have a char array as an initialiser. C doesn't allow array values in expressions.

That it allows this at all:

char s[8] = "ABCDEFG";

is enough of a quirk. (The alternative would be laboriously typing {'A', 'B', 'C', ..., 'G', 0}.)

Maybe the language could have gone further and extended the quirk into the ?: term, but perhaps it was felt it had already strayed enough outside the language's type rules.

You can however do this:

#if COND()
char s[8] = "42";
#else
char s[8] ="4711";
#endif

Re: Initializing a char array with conditional expr

<87a67xzilq.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22571&group=comp.lang.c#22571

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Sun, 21 Aug 2022 17:06:41 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <87a67xzilq.fsf@bsb.me.uk>
References: <tdsccf$dbt$1@gioia.aioe.org>
<e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="a836a4cae643b17f016adf7dc2f2ba63";
logging-data="2507641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DWs7Av3wcwUr/AQMQfyk4sGSD4w1y1EM="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:Oa3a46cJgO7KmSpNv8sy83dpsR0=
sha1:wdQPPGj7iJavRY7ADYlNz+/btvE=
X-BSB-Auth: 1.30ab84ac12b7c46a6ffa.20220821170641BST.87a67xzilq.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 21 Aug 2022 16:06 UTC

bart c <bart4858@gmail.com> writes:

> On Sunday, 21 August 2022 at 05:30:24 UTC+1, Steve Keller wrote:
>
>> char s[8] = COND() ? "42" : "4711";
>
>> Why can't a char array have an expression as initializer?
>
> It needs to have a char array as an initialiser.

Not quite -- it must be a string literal. Any old char array won't do.

> C doesn't allow array values in expressions.

In both of

sizeof "a"
&"abc"

there is an array value in an expression. In fact, ordinary expressions
like

"abc"[0]
f("abc")

contain array values -- just ones that convert immediately to pointers.

--
Ben.

Re: Initializing a char array with conditional expr

<tdtv1q$2di6p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22572&group=comp.lang.c#22572

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Sun, 21 Aug 2022 11:54:47 -0700
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <tdtv1q$2di6p$1@dont-email.me>
References: <tdsccf$dbt$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Aug 2022 18:54:50 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="9991e656cb56f24d2eadd43e1b453366";
logging-data="2541785"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WV46TfXMuWz7Dx7tW3ULE"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.12.0
Cancel-Lock: sha1:BFXMAD4cfdSYGG54kbR0U/aSD64=
Content-Language: en-US
In-Reply-To: <tdsccf$dbt$1@gioia.aioe.org>
 by: Andrey Tarasevich - Sun, 21 Aug 2022 18:54 UTC

On 8/20/2022 9:30 PM, Steve Keller wrote:
>
> Why can't a char array have an expression as initializer?
>

Because the language is defined that way.

Naked arrays in C are generally non-copyable. So, you are not allowed to
initialize one array with another array, aside from one exception: you
are allowed to initialize a character array with a string literal.

This is a very narrow, very focused and very fragile exception from the
general rule. The moment you change anything, the exception no longer
applies. In your case the RHS of the initialization is "some
expression", not a string literal. So, the exception no longer applies
and we are back to the general rule: arrays are non-copyable in C.

--
Best regards,
Andrey

Initializing a char array with conditional expr

<te0h80$34e$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22603&group=comp.lang.c#22603

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!irENcRSlgsHJDCzuL+3UMQ.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Initializing a char array with conditional expr
Date: Mon, 22 Aug 2022 20:17:34 +0200
Organization: Aioe.org NNTP Server
Message-ID: <te0h80$34e$1@gioia.aioe.org>
References: <tdsccf$dbt$1@gioia.aioe.org> <e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
Injection-Info: gioia.aioe.org; logging-data="3214"; posting-host="irENcRSlgsHJDCzuL+3UMQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Mon, 22 Aug 2022 18:17 UTC

bart c <bart4858@gmail.com> writes:

> On Sunday, 21 August 2022 at 05:30:24 UTC+1, Steve Keller wrote:
>
> > char s[8] = COND() ? "42" : "4711";
>
> > Why can't a char array have an expression as initializer?
>
> It needs to have a char array as an initialiser. C doesn't allow
> array values in expressions.

OK, I see. A pity.

> You can however do this:
>
> #if COND()
> char s[8] = "42";
> #else
> char s[8] ="4711";
> #endif

I'd like to, and have done similar things often before. In this case,
however, my condition can't be evaluated by the preprocessor. I'd
like to check whether char is signed or unsigned. I'll probably
change s[] to *s although I'd prefer s[] in this particular case.

Steve

Re: Initializing a char array with conditional expr

<te0j1n$2m2e8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22604&group=comp.lang.c#22604

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Mon, 22 Aug 2022 18:48:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <te0j1n$2m2e8$1@dont-email.me>
References: <tdsccf$dbt$1@gioia.aioe.org>
<e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
<te0h80$34e$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Aug 2022 18:48:23 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="cdb293a4546b2370d72ba4bcb6f7eda2";
logging-data="2820552"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3N/XzAbIlpI7osJXuZmqogIcJY3j16Dw="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:jKBValdTwg5R/dDHecmzcx7/Lj0=
 by: Lew Pitcher - Mon, 22 Aug 2022 18:48 UTC

On Mon, 22 Aug 2022 20:17:34 +0200, Steve Keller wrote:

> bart c <bart4858@gmail.com> writes:
>
>> On Sunday, 21 August 2022 at 05:30:24 UTC+1, Steve Keller wrote:
>>
>> > char s[8] = COND() ? "42" : "4711";
>>
>> > Why can't a char array have an expression as initializer?
>>
>> It needs to have a char array as an initialiser. C doesn't allow array
>> values in expressions.
>
> OK, I see. A pity.
>
>> You can however do this:
>>
>> #if COND()
>> char s[8] = "42";
>> #else
>> char s[8] ="4711";
>> #endif
>
> I'd like to, and have done similar things often before. In this case,
> however, my condition can't be evaluated by the preprocessor. I'd like
> to check whether char is signed or unsigned.

Wouldn't
#include <limits.h>
#if (CHAR_MIN < 0)
char s[8] = "signed";
#else
char s[8] = "unsigned";
#endif
work for you?

> I'll probably change s[]
> to *s although I'd prefer s[] in this particular case.
>
> Steve

--
Lew Pitcher
"In Skills, We Trust"

Re: Initializing a char array with conditional expr

<te0mns$2oa1m$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22607&group=comp.lang.c#22607

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Mon, 22 Aug 2022 21:51:23 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <te0mns$2oa1m$1@dont-email.me>
References: <tdsccf$dbt$1@gioia.aioe.org>
<e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
<te0h80$34e$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Aug 2022 19:51:24 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="cc4a9dfb9007ede8eec6f290a1289f95";
logging-data="2893878"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19x9SQBRWzz93rzWyLB74qlSzNtKjKjfWs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:J8UZIVKVqp5MvKcdCUX13LrSrPE=
In-Reply-To: <te0h80$34e$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Mon, 22 Aug 2022 19:51 UTC

On 22/08/2022 20:17, Steve Keller wrote:
> bart c <bart4858@gmail.com> writes:
>
>> On Sunday, 21 August 2022 at 05:30:24 UTC+1, Steve Keller wrote:
>>
>>> char s[8] = COND() ? "42" : "4711";
>>
>>> Why can't a char array have an expression as initializer?
>>
>> It needs to have a char array as an initialiser. C doesn't allow
>> array values in expressions.
>
> OK, I see. A pity.
>
>> You can however do this:
>>
>> #if COND()
>> char s[8] = "42";
>> #else
>> char s[8] ="4711";
>> #endif
>
> I'd like to, and have done similar things often before. In this case,
> however, my condition can't be evaluated by the preprocessor. I'd
> like to check whether char is signed or unsigned. I'll probably
> change s[] to *s although I'd prefer s[] in this particular case.
>
> Steve

Out of curiosity, why do you need to check if char is signed or
unsigned? Why not use "signed char" and "unsigned char" explicitly when
it is relevant?

Re: Initializing a char array with conditional expr

<te23bu$1egq$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22622&group=comp.lang.c#22622

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!HldmDzaowyCSfT3p2Grddw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Tue, 23 Aug 2022 10:32:59 +0200
Organization: Aioe.org NNTP Server
Message-ID: <te23bu$1egq$1@gioia.aioe.org>
References: <tdsccf$dbt$1@gioia.aioe.org> <e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com> <te0h80$34e$1@gioia.aioe.org> <te0j1n$2m2e8$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="47642"; posting-host="HldmDzaowyCSfT3p2Grddw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Tue, 23 Aug 2022 08:32 UTC

Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

> Wouldn't
> #include <limits.h>
> #if (CHAR_MIN < 0)
> char s[8] = "signed";
> #else
> char s[8] = "unsigned";
> #endif
> work for you?

Ouch, that was seemingly too simple :-(( Yes, of course will that work.
I just didn't think of limits.h and instead tried (char)-1 >= 0.
Thanks.

Steve

Re: Initializing a char array with conditional expr

<te23g5$1gfn$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22623&group=comp.lang.c#22623

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!HldmDzaowyCSfT3p2Grddw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Tue, 23 Aug 2022 10:35:14 +0200
Organization: Aioe.org NNTP Server
Message-ID: <te23g5$1gfn$1@gioia.aioe.org>
References: <tdsccf$dbt$1@gioia.aioe.org> <e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com> <te0h80$34e$1@gioia.aioe.org> <te0mns$2oa1m$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="49655"; posting-host="HldmDzaowyCSfT3p2Grddw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Tue, 23 Aug 2022 08:35 UTC

David Brown <david.brown@hesbynett.no> writes:

> Out of curiosity, why do you need to check if char is signed or
> unsigned? Why not use "signed char" and "unsigned char" explicitly
> when it is relevant?

It's a very simple tool, that prints out sizes of some types, offsets
of some struct members, and for char, whether it's sigend or unsigned.
I sometimes only cross-compile it looking at the asm output without
running it. Therefore, having the strings directly in an array s[]
would be a bit easier than having to follow the pointer *s.

Steve

Re: Initializing a char array with conditional expr

<te2kp5$30e5h$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22628&group=comp.lang.c#22628

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Initializing a char array with conditional expr
Date: Tue, 23 Aug 2022 15:30:12 +0200
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <te2kp5$30e5h$1@dont-email.me>
References: <tdsccf$dbt$1@gioia.aioe.org>
<e04f3718-736a-487e-a9ab-11582438482bn@googlegroups.com>
<te0h80$34e$1@gioia.aioe.org> <te0mns$2oa1m$1@dont-email.me>
<te23g5$1gfn$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 23 Aug 2022 13:30:13 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="672e5cdaeacdccedbaf9606c19675aef";
logging-data="3160241"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mnjtJfGZK7kf/PT0HIh/8QPgoMLvLB1Q="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:WCXEybWcyPyuPIu9GKrtpHaeYDk=
Content-Language: en-GB
In-Reply-To: <te23g5$1gfn$1@gioia.aioe.org>
 by: David Brown - Tue, 23 Aug 2022 13:30 UTC

On 23/08/2022 10:35, Steve Keller wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> Out of curiosity, why do you need to check if char is signed or
>> unsigned? Why not use "signed char" and "unsigned char" explicitly
>> when it is relevant?
>
> It's a very simple tool, that prints out sizes of some types, offsets
> of some struct members, and for char, whether it's sigend or unsigned.
> I sometimes only cross-compile it looking at the asm output without
> running it. Therefore, having the strings directly in an array s[]
> would be a bit easier than having to follow the pointer *s.
>
> Steve

Why not have a series of declarations like :

const int sizeShort = sizeof(short);
const int sizeInt = sizeof(int);

const int charMinus1 = (char) -1;

and compile that, or paste it into <https://godbolt.org> (which has
compiler options for a fair number of targets) ?

You can test it with gcc (for lots of targets) using the "-fsigned-char"
or "-funsigned-char" options to see the difference.

I find the best idea for writing code when you want control of the sizes
of types (and when do you /not/ want that?) is to use the types in
<stdint.h>. It's very rare that I need to care about the size of
"short" or "long", as I use uint16_t, int32_t, uintptr_t, as required by
the needs of the code. And of course uint8_t and int8_t instead of
worrying about the sign of "char". If that's not to your liking, then
<limits.h> can give you plenty of compile-time help.

Of course, knowing the sizes on a particular implementation can help
understanding code written by someone else.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor