简体   繁体   English

在Objective-C中将strcpy与字符数组和字符串值一起使用时遇到问题

[英]Trouble using strcpy with character array and string values in Objective-C

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." 我正在Objective-C上一门课程,并被告知要“创建一个名为名称的字符数组。用五个名填充该数组。使用strcpy命令将字符串值复制到该数组中。”

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. 我已经使用此代码35分钟了,但仍然无法运行或没有错误。

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. 我不明白为什么它不能正常工作,因为我用24分配了正确数量的char索引,并且觉得我也正确使用了strcpy函数。

Here are the errors that I'm getting in xcode: 这是我在xcode中遇到的错误:

  1. Expected ";" 预期为“;” after expression - wants to place ; 表达之后-想摆放; after strcpy 在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. 任何帮助,我们将不胜感激。

strcpy takes only two arguments. strcpy仅接受两个参数。

You should do as: strcpy (names, "Jeff"); 您应该这样做: strcpy (names, "Jeff");


I think you want to store 5 names in an array. 我认为您想在一个数组中存储5个名称。 For this you need to create a 2D-array. 为此,您需要创建一个2D阵列。

char names[5][24];

And then use: 然后使用:

strcpy (names[0], "Jeff");        
strcpy (names[1], "Steve");
...//etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM