Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #164616
| 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:56 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <ssp33a$aq9$1@reader1.panix.com> (permalink) |
| References | <ssos8v$pq3$1@reader1.panix.com> <86mtjjnale.fsf@linuxsc.com> |
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> John Forkosh <forkosh@panix.com> writes:
>
>> Minimal working (or should I say non-working example)...
>>
>> #include <stdio.h>
>> int main () {
>> int istest=1;
>> char op[8] = (istest?"ef":"df");
>> /*char op[8] = "ef";*/
>> printf("op=%s\n",op); }
>>
>> for which cc emits the error...
>>
>> test.c: In function 'main':
>> test.c:4:16: error: invalid initializer
>> 4 | char op[8] = (istest?"ef":"df");
>> | ^
>>
>> But if I comment out that line and uncomment the one below it,
>> then it compiles and runs fine. I've used this construction
>> a million times without problems. But a-million-and-one seems
>> to somehow be a problem. What's wrong???
>
> What you've done is take a construct (the istest?"ef":"df"
> subexpression) that works in lots of contexts, and then tried to
> use it in a different context where it doesn't work.
>
> The '=' in a declaration is not an assignment but rather is used
> for initialization. Assignment and initialization are similar
> in many ways but not in all ways.
>
> For the declaration of 'op', what is being initialized is an
> array. Initializers for arrays usually are written using a
> brace-enclosed list of values, such as
>
> char op[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
>
> For arrays of character type, C also allows two special forms
>
> char op[8] = "foo";
> or
> char op[8] = { "foo" };
>
> The declaration of op[8] in your program doesn't use any of those
> three forms, so the compiler complains.
>
> To get what you want, you could write a rather cumbersome array
> initializer:
>
> char op[8] = { istest ? 'e' : 'd', 'f' };
Thanks, Tim. That's certainly ingenious. After reading Bart's
answer, I first tried thinking/trying various syntactic fixes
to get the compiler to do what I wanted. But nothing I tried
worked, and your solution, which indeed works, never crossed
my mind.
> Alternatively, you could declare op[8] without an initializer and
> subsequently use a string function such as strcpy() to provide
> values to array elements (which in some cases will be only some
> of the array elements, but presumably you don't care about that
> as long as there is a terminating null). This method is similar
> to the code in your followup posting.
Yeah, that's what I'm gonna do. No need to get fancy, or anything.
If I'd just realized my blunder, I'd just have done that to begin
with, and never posted any question. I only posted because the
problem completely baffled me. The strcpy() workaround was immediately
obvious.
> For this particular usage, strncpy() may be a better choice than
> strcpy(), as for example
>
> strncpy( op, istest?"ef":"df", sizeof op );
>
> because it will initialize all the elements of the array, and
> also is guaranteed not to write beyond the end of the array,
> which could happen if strcpy() were used (depending of course
> on what string is provided for the second argument).
Not any potential problem. The opcode syntax is fixed size,
and I've already allocated more than it'll ever need, precisely
for those "just in case" situations.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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