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


Groups > comp.lang.c > #164611

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-25 14:33 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ssp1nt$pq5$1@reader1.panix.com> (permalink)
References <ssos8v$pq3$1@reader1.panix.com> <ssosr4$pq3$2@reader1.panix.com> <ssovvk$urk$1@dont-email.me>

Show all headers | View raw


Bart <bc@freeuk.com> wrote:
> On 25/01/2022 13:09, John Forkosh wrote:
>> John Forkosh <forkosh@panix.com> wrote:
>> ... and just to emphasize the point,
>> the following minimal now-working program compiles
>> and runs just fine
>> 
>> #include <stdio.h>
>> #include <string.h>
>> int main () {
>>    int istest=1;
>>    /*char op[8] = (istest?"ef":"df");*/
>>    char op[8] = "ef";
>>    strcpy(op,(istest?"df":"ef"));
>>    printf("op=%s\n",op); }
>> 
>> So the (istest?"df":"ef") seems to be just fine,
>> as per my million-times-used expectations.
>> But the stoopid  char op[8] = (istest?"ef":"df");
>> is somehow creating a problem that completely
>> baffles me.
> 
> 
> You're initialising an array of small integers. This would normally be 
> done with:
> 
>    {'e', 'f'};
> 
> For an array of char, and for long strings, this would involve a lot of 
> typing. So C allows you to represent the list using a string:
> 
>    "ef";
> 
> even though this has the wrong type (char*, not char[]). It's easy to 
> see that you're pushing it if you further try to incorporate that into 
> an expression. Partly because the expression has to yield a value-array 
> type (char[]), which is not possible in C.
> 
> The standards experts will be along shortly to tell you exactly which 
> sub-section and paragraph forbids it.
> 
> Meanwhile, try changing 'char op[8]' into 'char* op', and it should 
> work. Your version using strcpy is manipulating char* types not char[], 
> that's why it works.

Thanks, I know for sure I've used more-or-less this type
of construction a million times, and could've sworn I'd done it
exactly this way at least a few thousand, but apparently not.
And just to check your remarks, while the  char op[8] = "ef";
initialization works, when I tried what you'd (or at least me'd)
think would be the same  char op[8] = (char *)"ef";
then that emitted the same invalid initializer error.
  No problem, I'll just use the strcpy. I usually allocate more
bytes than I actually need in these kinds of cases, just in case
an extra character or two slips in. So I don't personally like
the char *op= solution. Anyway, thanks again for clearing it up.
-- 
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