Question

I am taking a course in Objective-C and was told to "Create a character array called names. Populate the array with five first names. Use the strcpy command to copy the string values in to the array."

I coded my solution to this but it keeps giving me errors. I have been playing with this code for 35 minutes now and still cannot get it to run or be error free.

Here is the code that I created in response to the instructions above:

  char names[24];
  strcpy (names, "Jeff", "Steve", "Stan", "Mike", "Travis");

I do not understand why it won't work properly because I have allocated the correct number of char indexes with 24 and feel that I have used the strcpy function correctly as well.

Here are the errors that I'm getting in xcode:

  1. Expected ";" after expression - wants to place ; after strcpy
  2. Too many arguments provided to function-like macro invocation.
  3. Extranneous ")" before ";" - Wants me to replace ")" with quotes "

Any help with this is greatly appreciated.

Was it helpful?

Solution

strcpy takes only two arguments.

You should do as: strcpy (names, "Jeff");


I think you want to store 5 names in an array. For this you need to create a 2D-array.

char names[5][24];

And then use:

strcpy (names[0], "Jeff");        
strcpy (names[1], "Steve");
...//etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top