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.

  1. your main() prototype wrong, should int main(int argc, char *argv[]); or equivalent. dropping initial int argument not ok.
  2. you're declaring strs array of character pointers (without valid size!), want full 2d array of chars, char strs[100][32];. limited, simpler manage.
  3. you're storing characters, need keep track of character index current 1 in current string. strs[i] = c; should strs[i][j++] = c;. of course must respect maximum length each string, , terminate string properly.
  4. you need step next string (increment i) on newline.

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -