How do you store an array of strings in C and print it back? -
i studying 'the c programming language' brian kerningham & dennis ritchie.
i'm stuck on 1.9 character arrays.
i'm trying allow user input multiple lines of text cmd stored in array of strings parameter use program. each new line should stored new object in array. want print array cmd can see working correctly, ideas doing wrong?
#include <stdio.h> int main(char string[]) { int c, i; char * strs[i]; (i=0; i<5 && (c!=eof()) && c!='\n'; i++){ strs[i] = c; } for(i=0; i<5; ++i) puts(strs[i]); }
your code has quite number of mistakes in it.
- your
main()
prototype wrong, shouldint main(int argc, char *argv[]);
or equivalent. dropping initialint
argument not ok. - you're declaring
strs
array of character pointers (without valid size!), want full 2d array of chars,char strs[100][32];
. limited, simpler manage. - you're storing characters, need keep track of character index current 1 in current string.
strs[i] = c;
shouldstrs[i][j++] = c;
. of course must respect maximum length each string, , terminate string properly. - you need step next string (increment
i
) on newline.
Comments
Post a Comment