gpt4 book ai didi

c - 在 C 中标记字符串

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:53 24 4
gpt4 key购买 nike

我正在尝试将一个带有名字和姓氏的字符串从一个字符串存储到一个结构中,但我得到了(警告:传递 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, ",");

}


}

最佳答案

忽略代码中的(许多)错误,您将 strcpy() 放在正确的位置。

但是,您没有使用正确的参数调用它:strcpy() 需要 2 个参数。
基本上,两者都是 char* 类型;您正在传递一个 char 和一个 char* ,这就是编译器提示的原因(对于编译器来说, char 的行为就像一个 int 所以它说“strcpy makes pointer from integer”)。

您需要检查您的数据结构并将正确的 char* 传递给 strcpy()

关于c - 在 C 中标记字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5444337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com