How do you create a dynamic array of strings? Like if I had this code printf("What are the names of your pets?");

what piece of code would I write to get multiple string answers from the user? (I'm sorry if the answer is obvious I'm just a beginner at C programming)

1 Answer 1


You can ask user to separate names with comma or space or some other delimiter. You can find strchr() function useful for parsing names in such an input.

Another option is to ask further names in a loop by saying press y(or some character) to add another name.

  • I don't fully understand, if it isn't a bother can you provide some sample code? like for the situation of the pets? Dec 24, 2016 at 23:24
  • You can start with printf("What are the names of your pets? (Enter them by separating with commas)"); string petNames = GetString(); // parse individual names by using strchr, seaching for commas, let me now where you get stuck.
    – t.m.
    Dec 25, 2016 at 7:14
  • so I created a pastebin(put the code into the cs50 ide and test it with the example I give to see what I mean.) of the code. so we're getting somewhere, but we still have problems, like if petNames is "steve, spots, Bailey" than the output would be ",spots,bailey ,bailey" skipping steve, how can I force a ',' at the begging of the petNames string so that the output would be "steve, spots, bailey ,spots, bailey ,bailey" than how could we completely get rid of ','? Dec 25, 2016 at 19:47
  • strchar() just returns a pointer to first occurance of comma there. If you are not familiar with pointers, it has no use for you.
    – t.m.
    Dec 25, 2016 at 19:54
  • Is this question related to CS50? If it's not, you can ask your questions at stackoverflow where you can get faster response.
    – t.m.
    Dec 25, 2016 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .