在 C 中标记字符串

发布于 2024-10-27 13:27:34 字数 822 浏览 4 评论 0原文

我正在尝试将带有名字和姓氏的字符串从字符串存储到结构中,但我得到了(警告:传递 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-11-03 13:27:34

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

但是,您没有使用正确的参数调用它:strcpy() 需要 2 个参数。
基本上,两者都是 char* 类型;您传递的是 charchar*,这就是编译器抱怨的原因(对于编译器来说,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 a char and a char* and that is why the compiler complains (for the compiler the char behaves like an int so it says "strcpy makes pointer from integer").

You need to review your data structure and pass the right char*s to strcpy().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文