在 C 中将 strcpy 与字符串数组一起使用
我有一个字符数组定义如下: char *c[20];
我正在尝试执行以下操作: strcpy(c[k], "undefined);
但它不起作用
我也尝试将其定义为 char c[20][70]
code> 没有运气。
编辑:我实际上知道它是一个字符数组的数组,我需要这样。
I have a character array defined as follows: char *c[20];
I'm trying to do: strcpy(c[k], "undefined);
but it's not working
I've also tried defining it as char c[20][70]
with no luck.
Edit: I actually know it's an array of character arrays, I need it like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那不是一个字符数组;这是一个字符指针数组。删除
*
以使其成为字符数组:然后您可以使用 strcpy:
如果您确实想要一个字符数组数组,则必须执行您所说的尝试:
问题可能有您错过了结束
"
,但我不知道这是否是粘贴错误。或者,问题可能是您使用k
而没有 没有看到您收到的错误消息,我们就无法知道。定义它,如果 你想将它们全部设置为该字符串,然后循环它们:
That's not a character array; that's an array of character pointers. Remove the
*
to make it a character array:Then you can use strcpy:
If you really did want an array of character arrays, you'll have to do what you said you tried:
The problem could have been you're missing the closing
"
, I don't know if that was a paste error though. Or, the problem could have been that you're usingk
without defining it, we can't know without seeing the error message you get.If you want to set them all to that string, then just loop over them:
如果您想要的是 20 个字符串,每个字符串包含 70 个字符,那么您的第二个选项应该有效:
char *c[20]
定义不正确,因为您只是定义了一个包含 20 个指针的数组,并且指针未初始化。你可以通过这种方式让它工作:If what you want is to have 20 strings of 70 chars each then your second option should work:
The
char *c[20]
definition is incorrect because you are just defining an array of 20 pointers, and the pointers are not initialized. You could make it work in this way: