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


Groups > comp.lang.c > #164640

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:37 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ssr17f$8ao$3@reader1.panix.com> (permalink)
References <ssos8v$pq3$1@reader1.panix.com> <ssqgd4$3ab$1@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
> Not sure about the intent of your question. Are you asking how it 
> contradicts rules of the language? Or are you asking why the rules of 
> the language are written that way?

Neither... just asking "if it contradicts". I'd been sure it was okay,
and spent like 5 minutes looking for a missing ; or something to
explain why it was throwing the error. Only when I gave up and
separately wrote that 5-line "minimal (non-)working example" did
I realize it was itself an error.

> The answer to the former is that each string literal ("ef" and "df") is 
> an array. Arrays in C are affected by so called "array type decay": they 
> turn into pointers, with the exception of several immediate contexts
> 
>   1. Operand of unary `&`
>   2. Operand of `sizeof`
>   3. Operand of `_Alignof`
>   4. String literal initializer for a `char[]` array
> 
> This why your commented-out line compiles: it is excepted from the decay 
> by point 4 and handled in a very special way, described in the standard.
> 
> Your original declaration is not excepted by any of these rules. So, 
> your literals decay to pointers. And pointers cannot be used to 
> initialize arrays. End of story.
> 
> As for why the language standard is written that way... Well, that's 
> just the way it works in C.

Yeah, "why" stopped bothering me long ago. There are various grammatical
rules that don't quite jibe with my personal sensibilities, but I've
got more pressing issues than arguing about them. Just need to know what
they are, which in this case I didn't.
-- 
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