Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Splitting strings into arrays with sscanf Date: Fri, 16 Jun 2017 12:20:11 -0700 Organization: None to speak of Lines: 42 Message-ID: References: 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="U2FsdGVkX18z+HtcbdeWqjDvCw9ZcAfS" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:5Zc3VJeOrDStqaK44nNRK4SedlQ= sha1:LHm9qTdjkhbYNaXmSrRXDEAOzY8= Xref: csiph.com comp.lang.c:112366 scott@slp53.sl.home (Scott Lurndal) writes: > DFS writes: >>How do you use sscanf to split a line into pieces and add each piece to >>an array? >> >>#include >>#include >>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 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"