-1
#include<stdio.h>
int main()
{
   int w,p;
   char *name[1000];
   for(w=0; p!=2; w++)
   {
      printf("Add a name: ");
      gets(name[w]);
      printf("Want to add another one?\n1 for yes\n2 for no\n");
      scanf("%d",&p);
   }
}

This code is not working. If I write: char *name[3]={"Mitchell Johnson", "Mitchell Starc", "Steven Smith"}; then it works. but i want to take strings as input. dont want to define it. But console box stops working after taking 1 string input. How to do it right? Please help. Thanks.

jake
  • 19
  • 1
  • 6
  • 4
    [why-is-the-gets-function-so-dangerous-that-it-should-not-be-used](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – LPs Mar 23 '17 at 14:11
  • 1
    You should explain what is your objective: store many strings or overwrite a single buffer with different strings? – LPs Mar 23 '17 at 14:16
  • 1
    @LPs `p<=2` would continue looping when the user enters `2`. – Emil Laine Mar 23 '17 at 14:30

2 Answers2

1
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
   int w = 0;
   int ret = 0;
   int p = 1;
   char line[1024];
   char **tab = (char**)malloc(1000*sizeof(char*));
   for(w=0; p==1; w++)
   {
      printf("Add a name: ");
      ret = read(0, &line, 1024);
      line[ret] = 0;
      printf(line);
      tab[w] = strdup(line);
      printf("Want to add another one?\n1 for yes\n2 for no\n");
      scanf("%d",&p);
   }
}

This should do what you want. But it's pretty ugly.

Groskilled
  • 61
  • 1
  • 7
  • thanks, it works!! but i am not getting how this thing works. can make it simpler? i am very beginner in C programming. please. can u make my code work (the one that i included in question) by doing some changes? – jake Mar 23 '17 at 14:40
  • @Adder yes, that why i said it's ugly. About the code it is pretty simple: First I create everything i will need and initialize it (very important !). The char line[1024] is just a buffer in wich I will write the line written. The char **tab is an array in wich we copy the last line at the end (which is w). You can find the read function in man 2 or somewhere on the web, it returns the number of char read. That is why I add a 0 after the last char with line[ret] = 0; And tab[w] = strdup(line) is just to allocate memory and copy the string line at the position w in the array. – Groskilled Mar 23 '17 at 14:48
  • By the way, if you type a string which countains more than 1024 char it will be truncated and if you enter more than 1000 lines, it will probably crash. To avoid that you need to create a new array of string which is bigger than the previous and copy the previous in the new one and use the new one. – Groskilled Mar 23 '17 at 14:51
  • thank you. now i started getting it – jake Mar 23 '17 at 14:55
0

You forget to allocate memory for the string read:

#include<stdio.h>
int main()
{
   int w,p=0;
   char *name[1000];
   char line[1024];
   for(w=0; p!=2; w++)
   {
      printf("Add a name: ");
      fgets(line, 1024,stdin);
      name[w]= malloc(strlen(line)+1);  // add this
      strcpy(name[w], line);            // add this
      printf("Want to add another one?\n1 for yes\n2 for no\n");
      scanf("%d",&p);
   }
}

Note that you must initialize p; it could already be 2.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41