Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Why is this outputting alternate blank lines? |
| Date | 2016-12-15 22:08 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87k2b04zgv.fsf@bsb.me.uk> (permalink) |
| References | (2 earlier) <o2s0sb$tb$1@dont-email.me> <87vaum5km7.fsf@bsb.me.uk> <o2shtc$108$1@dont-email.me> <87k2b259fk.fsf@bsb.me.uk> <f06d048a-b924-4a3f-a13d-96aaaa93a935@googlegroups.com> |
mark.bluemel@gmail.com writes:
> On Thursday, 15 December 2016 00:21:31 UTC, Ben Bacarisse wrote:
>> I'm correcting a mistake I made and adding something I missed...
>>
>> DFS <nospam@dfs.com> writes:
>>
>> > Using your recommendations:
>> >
>> > //copy file data into array
>> > char *datestr=malloc(linecount*sizeof(*datestr));
>> > rewind(fp);
>> > while(fgets(line,linewidth,fp)!=NULL) {
>> > strcpy(datestr[i],line);
>> > i++;n++;
>> > }
>> >
>> >
>> > Gives me a warning and a note:
>> >
>> > readdates.c:46:10: warning: passing argument 1 of ‘strcpy’ makes
>> > pointer from integer without a cast [-Wint-conversion]
>> > strcpy(datestr[i],line);
>> > ^~~~~~~
>>
>> Sorry, my mistake
>>
>> char **datestr = malloc(linecount * sizeof *datestr);
>>
>> You need datestr to be a pointer to the first element of an allocated
>> array of character pointers.
>
> You've still got the problem that the strcpy will be copying to a destination
> specified by an uninitialized pointer, won't you?
Yes that's true in the code above (which I just skimmed over) but the
original had
datestr[i]=malloc(linewidth);
strcpy(datestr[i],line);
in the loop. DFS must have taken my suggestion to allocate an array of
pointers (rather than declaring one), as meaning that no further malloc
calls are needed.
DFS: I was talking only about replacing the declaration will this
allocation. The other allocations need to stay.
Thanks for spotting that DFS must have misunderstood me. I missed it.
> If DFS is targeting a POSIX-system, something like this should work, I
> think (untried).
>
> char **datestr=malloc(linecount*sizeof(*datestr));
> rewind(fp);
> while(fgets(line,linewidth,fp)!=NULL) {
> datestr[i] = strdup(line);
Yes, and DFS can can always write a copy_sting function for greater
portability.
> i++;n++;
> }
>
> Otherwise, something like this is needed (again untested) :-
>
> char **datestr=malloc(linecount*sizeof(*datestr));
> rewind(fp);
> while(fgets(line,linewidth,fp)!=NULL) {
> datestr[i] = malloc(sizeof(datestr));
Not sure what you are getting at here because sizeof(datestr) is not
ebough. The original malloc(linewidth) which is fine, if a little
pessimistic. Copying the string by allocating the right length (as you
suggest) is the way to go in general.
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-13 23:18 -0500
Re: Why is this outputting alternate blank lines? Melzzzzz <mel@zzzzz.com> - 2016-12-14 05:30 +0100
Re: Why is this outputting alternate blank lines? mark.bluemel@gmail.com - 2016-12-14 00:59 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 10:38 +0000
Re: Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-14 12:53 -0500
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 18:05 +0000
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 11:57 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 20:05 +0000
Re: Why is this outputting alternate blank lines? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-14 15:21 -0500
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 12:28 -0800
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-14 13:09 -0800
Re: Why is this outputting alternate blank lines? Jean-Marc Bourguet <jm@bourguet.org> - 2016-12-14 22:53 +0100
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-14 15:47 -0800
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 15:41 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 23:26 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 17:33 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 00:42 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-14 17:05 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:21 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-15 09:15 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 17:50 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-15 12:15 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 20:37 +0000
Re: Why is this outputting alternate blank lines? raltbos@xs4all.nl (Richard Bos) - 2016-12-15 20:08 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 20:40 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:36 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-15 17:43 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 01:05 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-15 18:34 -0700
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-15 10:14 -0800
Re: Why is this outputting alternate blank lines? raltbos@xs4all.nl (Richard Bos) - 2016-12-15 20:15 +0000
Re: Why is this outputting alternate blank lines? Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 01:51 -0800
Re: Why is this outputting alternate blank lines? asetofsymbols@gmail.com - 2016-12-15 06:09 -0800
Re: Why is this outputting alternate blank lines? Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 17:47 -0800
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 12:15 -0800
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-15 10:24 -0800
Re: overcommit is the plague Noob <root@127.0.0.1> - 2016-12-17 01:17 +0100
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 20:19 +0000
Re: Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-14 17:44 -0500
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 00:21 +0000
Re: Why is this outputting alternate blank lines? mark.bluemel@gmail.com - 2016-12-15 00:56 -0800
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 22:08 +0000
Re: Why is this outputting alternate blank lines? Geoff <geoff@invalid.invalid> - 2016-12-14 13:39 -0800
Re: Why is this outputting alternate blank lines? Ike Naar <ike@iceland.freeshell.org> - 2016-12-16 07:12 +0000
csiph-web