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


Groups > comp.lang.c > #112366

Re: Splitting strings into arrays with sscanf

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Splitting strings into arrays with sscanf
Date 2017-06-16 12:20 -0700
Organization None to speak of
Message-ID <ln37azbuz8.fsf@kst-u.example.com> (permalink)
References <oi14f9$edg$3@dont-email.me> <kQU0B.45$9D5.31@fx12.iad>

Show all headers | View raw


scott@slp53.sl.home (Scott Lurndal) writes:
> DFS <nospam@dfs.com> writes:
>>How do you use sscanf to split a line into pieces and add each piece to 
>>an array?
>>
>>#include <stdio.h>
>>#include <string.h>
>>int main (int argc, char *argv[])
>>{
>>	char line[50]    = "";
>>	char lbls[4]     = "";
>>	char data[10][4] = "";
>
> The initializations don't match the declarations, and
> while lines 5 & 6 don't get errors, they won't provide you
> with the results you desire.   Instead, lose the initializers
> and use memset to initialize the vectors to your desired value(s).
>
> e.g.
>
>  char data[10][4];
>
>  memset(data, 0, sizeof(data));

The initializers for `line` and `lbls` are perfectly valid.  Only the
initialization for `data` is incorrect.  One way of fixing it is:

    char line[50]    = "";
    char lbls[4]     = "";
    char data[10][4] = { "" }; /* remaining elements are implicit */

or even:

    char line[50]    = { 0 };
    char lbls[4]     = { 0 };
    char data[10][4] = { 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


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