Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #112367
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Splitting strings into arrays with sscanf |
| Date | Fri, 16 Jun 2017 12:28:40 -0700 |
| Organization | None to speak of |
| Lines | 72 |
| Message-ID | <lny3srag0n.fsf@kst-u.example.com> (permalink) |
| References | <oi14f9$edg$3@dont-email.me> <kQU0B.45$9D5.31@fx12.iad> <oi168c$l43$2@dont-email.me> <WeV0B.2768$AG1.49@fx42.iad> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="512e3ac5c0dae2d372e7b11219066eb5"; logging-data="4060"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Rk5wtpVSQHQbVH0ckZ462" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:vNWK8mlYA2SJqmk5YlzFC2U4bpI= sha1:OBOCzJfV+4gCCDit9bZ0OnfKd2s= |
| Xref | csiph.com comp.lang.c:112367 |
Show key headers only | View raw
scott@slp53.sl.home (Scott Lurndal) writes:
> DFS <nospam@dfs.com> writes:
[...]
>>Done, and then it compiles, but when run it crashes on line 14:
>
> In C, a string is an array of characters terminated by
> an ASCII (or UTF-8) NUL byte.
C doesn't mention ASCII (other than a couple of non-normative
footnotes). A string is terminated by a null character, i.e., a
character with the value zero ('\0').
[...]
>> char line[50] ;
>> char lbls[4] ;
>
> This declaration reserves four bytes of memory (four 1-byte characters).
>
>> char data[10][4] ;
>>
>> memset(lbls, 0, sizeof(lbls));
>
> This sets all four bytes to zero.
Which means that lbls contains a string of length 0. The original code:
char lbls[4] = "";
did exactly the same thing.
>> memset(data, 0, sizeof(data));
>>
>> strcpy(line,"Name,Quarter,Product,Sales");
>> sscanf(line, "%s,%s,%s,%s", lbls[0],lbls[1],lbls[2],lbls[3]);
>
> This line (the scanf) is the root of your problem. The arguments to
> scanf when the format character is '%s' must be the first address of a
> character vector sized sufficiently to hold the value being scanned.
>
> Passing lbls[0] passes the value zero as the argument. When sscanf
> processes the first '%s', it will take that zero and use it as the
> address of the first byte in which to store the matching data. Since
> the value zero maps to the NULL pointer on pretty much all current
> systems, you'll get a segmentation violation when attempting to store
> the result.
More simply, "%s" requires an argument of type char*, but it's being
given an argument of type int (lbls[0] is of type char, which promotes
to int). The behavior is undefined.
> If you had, instead, written:
>
> sscanf(line, "%s,%s,%s,%s", &lbls[0], &lbls[1], &lbls[2], &lbls[3]);
>
> Then the correct 'char *' arguments would have been passed to the sscanf
> function. However, there is only one byte of storage available at each
> of those pointers, so you'll end up corrupting your stack.
&lbls[0] points to the beginning of a 4-byte object, but the other
arguments point to overlapping locations within that object. If you wrote:
sscanf(line, "%s", &lbls[0]);
it would be fine as long as the scanned data is no more than 3 bytes
long (an additional byte is needed for the trailing '\0').
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 13:33 -0400
Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 17:46 +0000
Re: Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 14:02 -0400
Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 18:15 +0000
Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:28 -0700
Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:20 -0700
Re: Splitting strings into arrays with sscanf Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-16 13:47 -0400
Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 18:01 +0000
Re: Splitting strings into arrays with sscanf Barry Schwarz <schwarzb@dqel.com> - 2017-06-16 12:03 -0700
Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 15:33 -0400
Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 13:16 -0700
Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:31 -0700
Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 15:45 -0400
Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 14:07 -0400
Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 14:22 -0400
Re: Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 15:36 -0400
Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 17:17 -0400
Re: Splitting strings into arrays with sscanf Barry Schwarz <schwarzb@dqel.com> - 2017-06-16 12:06 -0700
Re: Splitting strings into arrays with sscanf Real Troll <Real.Troll@Trolls.com> - 2017-06-16 17:30 -0400
Re: Splitting strings into arrays with sscanf ike@iceland.freeshell.org - 2017-06-17 16:49 +0000
Re: Splitting strings into arrays with sscanf Richard Damon <Richard@Damon-Family.org> - 2017-06-17 18:25 -0400
csiph-web