在 C 中标记字符串
我正在尝试将带有名字和姓氏的字符串从字符串存储到结构中,但我得到了(警告:传递 strcpy 的参数 1 使指针来自整数而不进行强制转换),并且我不确定在哪里将 strcpy 尝试将其放入 while 循环中得到了有意义的错误。但不确定在哪里放置 strcpy 已编辑
struct trip
{
char first_name;
char last_name;
}
int main(void)
{
struct trip travel[12];
}
char input_name(struct trip travel[MAXTRIP], int index)
{
int name_read, length;
int name_bytes = 100;
char *name, *word;
getchar();
printf("Please enter name:\n");
name = (char *)malloc(name_bytes + 1);
name_read = getline (&name, &name_bytes, stdin);
word = strtok(name, ",");
while (word != NULL)
{
strcpy(travel[index].first_name, word);
word = strtok(NULL, ",");
}
}
I'm trying to store a string with a first and last name from a string into a struct but I'm getting (warning: passing argument 1 of strcpy makes pointer from integer without a cast), and I'm not sure on where to put strcpy tried putting it in the while loop got the error which makes sense. But not sure on where to place strcpy
EDITED
struct trip
{
char first_name;
char last_name;
}
int main(void)
{
struct trip travel[12];
}
char input_name(struct trip travel[MAXTRIP], int index)
{
int name_read, length;
int name_bytes = 100;
char *name, *word;
getchar();
printf("Please enter name:\n");
name = (char *)malloc(name_bytes + 1);
name_read = getline (&name, &name_bytes, stdin);
word = strtok(name, ",");
while (word != NULL)
{
strcpy(travel[index].first_name, word);
word = strtok(NULL, ",");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
忽略代码中的(MANY)错误,您将
strcpy()
放在正确的位置。但是,您没有使用正确的参数调用它:
strcpy()
需要 2 个参数。基本上,两者都是
char*
类型;您传递的是char
和char*
,这就是编译器抱怨的原因(对于编译器来说,char
的行为类似于int
所以它说“strcpy 从整数生成指针”)。您需要检查数据结构并将正确的
char*
传递给strcpy()
。Ignoring the (MANY) errors in your code, you are putting the
strcpy()
in the right place.However, you are not calling it with the correct arguments:
strcpy()
needs 2 arguments.Basically, both are of type
char*
; you are passing achar
and achar*
and that is why the compiler complains (for the compiler thechar
behaves like anint
so it says "strcpy makes pointer from integer").You need to review your data structure and pass the right
char*
s tostrcpy()
.