Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #164642

Re: What's wrong with "char op[8]=(istest?"ef":"df");"

From John Forkosh <forkosh@panix.com>
Newsgroups comp.lang.c
Subject Re: What's wrong with "char op[8]=(istest?"ef":"df");"
Date 2022-01-26 09:22 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ssr3rs$m3n$1@reader1.panix.com> (permalink)
References <ssos8v$pq3$1@reader1.panix.com> <20220125204715.355@kylheku.com>

Show all headers | View raw


Kaz Kylheku <480-992-1380@kylheku.com> wrote:
> Initialization is not assignment, as others have answered.
> An array of char may be initialized by a literal, and that
> has special meaning. Note that arrays cannot be assigned
> at all in C:
>   char a[5];
>   a = "abc"; // error
> A solution which is semantically close to the apparent
> intended meaning of what you're trying to do is this:
>   char op[8] = { istest ? 'e' : 'd', 'f' };

Yeah, Tim already suggested this alternative. 
Way too ugly. Maybe okay as an isolated line,
but in 500+ lines you don't want to have to pause
for 5+ secs figuring out the intent of one little line.

> (This uses a non-constant expression for initializing an aggregate
> member, which was not required to work in ISO C C90, but became standard
> as of C99, at least for aggregates defined in automatic storage.)
> 
> By semantically close I mean that op is the the type that you seem to
> want: a modifiable array of 8 characters,

Yes, exactly. Glad you noticed, and I >>should<< have (but neglected
to) mentioned that earlier myself. char op[8]="ef"; where the 'f' means
read from a file, whereas after that initial read, subsequent passes
typically change op[1]='b' to read from an internal buffer.
So, as you point out below, char *op="ef"; compiles okay but won't
work... which is another kind of error I also discovered the hard way,
a long, long time ago (but in a relatively nearby galaxy).

> and that the goal of
> initializing that array one way or the other based on the flag is
> satisfied. Whereas:
>   char *op = istest ? "ef" : "df";
> is certainly succinct, op is not an array but a pointer.
> Therefore sizeof (op) calculates sizeof (char *) which is
> unrelated to how many characters there are in the underlyinga
> array. It points to an array which is not of 8 chars but 3, and
> that is not required to be modifiable; this is not well-defined
> behavior, under this definition of op:
>   op[0] = 'g';

You seem to have forgotten to add the subsequent "won't work" phrase,
though that's obviously what you meant. And thanks for mentioning it,
which prompted and reminded me to add my immediately preceding remarks.
And sorry to everybody else who suggested that char *op...; solution
for not mentioning it sooner.

> Another solution:
>   char op[8];
>   strcpy(op, istest ? "ef" : "df");
Yup, that's what I've done.

> If istest can be a compile-time condition:
>   #define istest 1
>   #if istest
>     #define iftest_init(THEN, ELSE) THEN
>     #define iftest(THEN, ELSE) (THEN)
>   #else
>     #define iftest_init(THEN, ELSE) ELSE
>     #define iftest(THEN, ELSE) (ELSE)
>   #endif
>   char op[8] = iftest_init("ef", "df");
> iftest() generates parentheses, which we want almost all the time,
> except here; so we introduce iftest_init() which doesn't do that.
> Defining "if" macros like this requires a bit of upfront verbiage,

You think??? Very cute, and maybe if I were initializing tons
of stuff this way it'd be useful. 

> but the resulting code can be preferrable to code like:
>   #if test
>     char op[8] = "ef";
>   #else
>     char op[8] = "df";
>   #endif
> or even
>   char op[8] =
>   #if test
>     "ef"
>   #else
>     "df"
>   #endif
>   ;

Actually, that second way is how I typically initialize stuff like
static int msglevel =
#if defined(MSGLEVEL)  /* if cc -DMSGLEVEL=nn */
  MSGLEVEL;
#else
  3;
#endif
(you recall the parameters on the jcl //job card?:)
-- 
John Forkosh  ( mailto:  j@f.com  where j=john and f=forkosh )

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-25 13:00 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-25 13:09 +0000
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" Bart <bc@freeuk.com> - 2022-01-25 14:03 +0000
      Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-25 14:33 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" Mateusz Viste <mateusz@xyz.invalid> - 2022-01-25 14:36 +0100
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-25 14:41 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-01-25 06:39 -0800
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-25 14:56 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-25 11:08 -0500
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-26 08:18 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-25 14:37 -0800
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-26 08:27 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-25 19:49 -0800
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-25 19:56 -0800
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-25 23:37 -0500
      Re: What's wrong with "char op[8]=(istest?"ef":"df");" Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-25 20:54 -0800
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-26 08:37 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" Kaz Kylheku <480-992-1380@kylheku.com> - 2022-01-26 05:08 +0000
    Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-26 09:22 +0000
  Re: What's wrong with "char op[8]=(istest?"ef":"df");" John Forkosh <forkosh@panix.com> - 2022-01-26 09:24 +0000

csiph-web