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


Groups > comp.lang.c > #164638

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 08:27 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ssr0ld$8ao$2@reader1.panix.com> (permalink)
References <ssos8v$pq3$1@reader1.panix.com> <87a6fjmogk.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> The "=" symbol can be used either in an assignment or in an declaration
> with an initializer.  They look similar, but they're different. For example:
>     int n = 42; // an declaration with an initializer
>     n = 42;     // an assignment
> The "=" in the declaration must be followed by an *initializer*, which
> can be either an expression or a sequence of one or more initializers
> enclosed in { ... }.  The "=" in the assignment must be followed by an
> expression. A declaration like
>     char op[8] = "ef";
> is a special case.  Arrays can be initialized, but they can't be
> assigned, so this:
>     op = "ef";
> is invalid.  The special case is that you can use a string literal
> as the initializer for an array object (of an appropriate type).
> There is no such special case for more complicated expressions like
> (istest?"ef":"df").
> 
> Somewhat similarly, but following from a different set of rules, this is
> valid:
>     int a[] = { 10, 20, 30 };
> but this is not:
>     int a[] = cond ? { 10, 20, 30 } : { 40, 50, 60 };
> because { 10, 20, 30 } is an initializer but not an expression.

Interesting example. And, yeah, I'd never think of trying to write
(cond?{10,20,30}:{40,50,60}) in any context. Just doesn't look right.
Actually, though I'm apparently prone to be wrong, I'd think there
isn't any context where that would compile without error.
-- 
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