Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #153099
| From | John Forkosh <forkosh@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: does char *str="abcd"; alloc addressable memory? |
| Date | 2020-07-07 07:00 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <re16hj$kan$2@reader1.panix.com> (permalink) |
| References | (1 earlier) <rdv1vh$idl$1@dont-email.me> <87k0zgbiwt.fsf@nosuchdomain.example.com> <re0oqg$4kj$3@reader1.panix.com> <873663c04v.fsf@nosuchdomain.example.com> <re15p4$kan$1@reader1.panix.com> |
John Forkosh <forkosh@panix.com> wrote:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> John Forkosh <forkosh@panix.com> writes:
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>> <snip>
>>>> Some compilers have non-conforming options to treat string literals as
>>>> const. (gcc has "-Wwrite-strings".)
>>>
>>> Wasn't aware of that warning, and sure enough...
>>> bash-5.0$ cc -Wwrite-strings tester.c -o tester
>>> tester.c: In function 'main':
>>> tester.c:7:15: warning: initialization discards 'const' qualifier
>>> from pointer target type [-Wdiscarded-qualifiers]
>>> 7 | char *str = ( argc>1? argv[1] : "01234&56789" );
>>> | ^
>>> And (as suggested downthread) changing line 7 to
>>> char *str = ( argc>1? argv[1] : (char *)"01234&56789" );
>>> eliminates the warning, but the program still segfaults.
>>>
>>> So, the really nice -switch would be one that reverts to the
>>> classical behavior where "string literals" aren't const.
>>> Any way to do that?
>>
>> I'm not aware of any such option.
>>
>> If you really want a writable copy of a string literal, you might
>> consider strdup(), which uses malloc() to create a copy of its argument
>> string. It's not C standard (though I think it's being added in the
>> next standard), but it is defined by POSIX -- and it's easy enough to
>> write your own version.
>>
>> Or just create a writable array object:
>>
>> char my_string[] = "01234&56789";
>>
>> I really wouldn't advise trying to modify the string literal itself,
>> even if you can find a compiler that lets you get away with it.
>
> Thanks, Keith. Yeah, I know a million (plus-or-minus 999,900 or so)
> ways to correctly/safely implement the functionality. Not a problem.
> As mentioned upthread, I use this kind of syntax mostly for small
> unit-test drivers, and prefer avoiding unnecessary lines of
> housekeeping, which just makes it harder to see through to the
> actual tests. And there are frequently half-a-dozen or more argv[]
> parameters, so the single-line method of grabbing them from the
> command line is minimal fuss, and easy to see the defaults.
Oh, wait, now I know a million-and-one ways. When you said
"...you might consider strdup()..."
I didn't immediately appreciate the significance of your
suggestion. But the simple change from...
char *str = ( argc>1? argv[1] : "01234&56789" );
to...
char *str = ( argc>1? argv[1] : strdup("01234&56789") );
immediately works. Thanks, that's the answer I was looking for.
--
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
does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-06 11:24 +0000
Re: does char *str="abcd"; alloc addressable memory? jacobnavia <jacob@jacob.remcomp.fr> - 2020-07-06 13:29 +0200
Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) gazelle@shell.xmission.com (Kenny McCormack) - 2020-07-06 12:48 +0000
Re: Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) BGB <cr88192@gmail.com> - 2020-07-06 11:55 -0500
Re: Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) Vir Campestris <vir.campestris@invalid.invalid> - 2020-07-06 21:21 +0100
Re: Mark Twain had something to say about this... John Forkosh <forkosh@panix.com> - 2020-07-07 03:21 +0000
Re: Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) mark.bluemel@gmail.com - 2020-07-09 06:30 -0700
Re: Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) Richard Damon <Richard@Damon-Family.org> - 2020-07-06 22:16 -0400
Re: Mark Twain had something to say about this... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-06 23:22 -0700
Re: Mark Twain had something to say about this... John Forkosh <forkosh@panix.com> - 2020-07-07 02:46 +0000
Re: Mark Twain had something to say about this... (Was: does char *str="abcd"; alloc addressable memory?) jacobnavia <jacob@jacob.remcomp.fr> - 2020-07-07 09:29 +0200
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-06 11:25 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 03:05 +0000
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-06 23:25 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 06:47 +0000
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 07:00 +0000
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-07 01:24 -0700
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-07 20:47 +0100
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-08 03:43 +0000
Re: does char *str="abcd"; alloc addressable memory? David Brown <david.brown@hesbynett.no> - 2020-07-07 09:35 +0200
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 07:54 +0000
Re: does char *str="abcd"; alloc addressable memory? David Brown <david.brown@hesbynett.no> - 2020-07-07 11:27 +0200
Re: does char *str="abcd"; alloc addressable memory? Richard Damon <Richard@Damon-Family.org> - 2020-07-09 06:53 -0400
Re: does char *str="abcd"; alloc addressable memory? David Brown <david.brown@hesbynett.no> - 2020-07-09 14:48 +0200
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-09 17:01 +0100
Re: does char *str="abcd"; alloc addressable memory? David Brown <david.brown@hesbynett.no> - 2020-07-09 22:10 +0200
Re: does char *str="abcd"; alloc addressable memory? scott@slp53.sl.home (Scott Lurndal) - 2020-07-09 20:23 +0000
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-09 23:56 +0100
Re: does char *str="abcd"; alloc addressable memory? antispam@math.uni.wroc.pl - 2020-07-10 03:25 +0000
Re: does char *str="abcd"; alloc addressable memory? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-07-10 11:56 -0400
Re: does char *str="abcd"; alloc addressable memory? richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-07-10 12:31 +0000
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-09 13:35 -0700
Re: does char *str="abcd"; alloc addressable memory? scott@slp53.sl.home (Scott Lurndal) - 2020-07-09 22:17 +0000
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-09 23:50 +0100
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-09 16:52 +0100
Re: does char *str="abcd"; alloc addressable memory? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-07-09 10:13 -0700
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-09 11:24 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-09 22:59 +0000
Re: does char *str="abcd"; alloc addressable memory? Manfred <noname@add.invalid> - 2020-07-10 16:36 +0200
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-10 15:54 +0100
Re: does char *str="abcd"; alloc addressable memory? Manfred <noname@add.invalid> - 2020-07-10 18:33 +0200
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-10 22:50 +0100
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-11 16:21 +0100
Re: does char *str="abcd"; alloc addressable memory? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-07-11 08:55 -0700
Re: does char *str="abcd"; alloc addressable memory? Bart <bc@freeuk.com> - 2020-07-11 17:09 +0100
Re: does char *str="abcd"; alloc addressable memory? Manfred <noname@add.invalid> - 2020-07-11 18:34 +0200
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-10 13:17 -0700
Re: does char *str="abcd"; alloc addressable memory? Manfred <noname@add.invalid> - 2020-07-11 02:00 +0200
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-10 17:46 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-11 03:09 +0000
Re: does char *str="abcd"; alloc addressable memory? Manfred <noname@add.invalid> - 2020-07-11 18:43 +0200
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-11 15:09 -0400
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-11 13:39 -0700
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-11 15:50 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-12 01:06 +0000
Re: does char *str="abcd"; alloc addressable memory? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-07-10 08:54 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-11 03:26 +0000
Re: does char *str="abcd"; alloc addressable memory? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-07-11 03:56 -0700
Re: does char *str="abcd"; alloc addressable memory? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-07-10 08:46 -0700
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-10 13:38 -0700
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-10 13:54 -0700
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-07 08:52 -0400
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 02:56 +0000
Re: does char *str="abcd"; alloc addressable memory? antispam@math.uni.wroc.pl - 2020-07-07 19:08 +0000
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-08 04:07 +0000
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-07 21:54 -0700
Re: does char *str="abcd"; alloc addressable memory? Ike Naar <ike@sdf.org> - 2020-07-06 11:56 +0000
Re: does char *str="abcd"; alloc addressable memory? Bonita Montero <Bonita.Montero@gmail.com> - 2020-07-06 13:57 +0200
Re: does char *str="abcd"; alloc addressable memory? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-07-06 05:37 -0700
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-06 19:52 +0100
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-06 12:20 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-07 03:32 +0000
Re: does char *str="abcd"; alloc addressable memory? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-07-07 06:26 -0700
Re: does char *str="abcd"; alloc addressable memory? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-07-07 10:03 -0700
Re: does char *str="abcd"; alloc addressable memory? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-07-09 10:07 -0700
Re: does char *str="abcd"; alloc addressable memory? John Forkosh <forkosh@panix.com> - 2020-07-08 03:57 +0000
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-08 11:02 -0400
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-06 09:43 -0700
Re: does char *str="abcd"; alloc addressable memory? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-07-06 19:35 +0100
Re: does char *str="abcd"; alloc addressable memory? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2020-07-06 18:50 -0700
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-06 08:35 -0400
Re: does char *str="abcd"; alloc addressable memory? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-07-06 11:21 -0400
Re: does char *str="abcd"; alloc addressable memory? Barry Schwarz <schwarzb@delq.com> - 2020-07-06 09:58 -0700
Re: does char *str="abcd"; alloc addressable memory? scott@slp53.sl.home (Scott Lurndal) - 2020-07-06 23:07 +0000
Re: does char *str="abcd"; alloc addressable memory? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-07-06 20:03 -0400
Re: does char *str="abcd"; alloc addressable memory? scott@slp53.sl.home (Scott Lurndal) - 2020-07-07 14:36 +0000
Re: does char *str="abcd"; alloc addressable memory? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-07-07 11:58 -0400
csiph-web