为什么我在 C 中遇到这个错误?不兼容的类型

发布于 2024-12-29 06:53:20 字数 333 浏览 2 评论 0原文

15:9:错误:从类型“char *”分配给类型“char[3]”时,类型不兼容

#include <stdio.h>


int main(int argc, char *argv[])
{

     char servIP[3];
     int servPortNum;
     if(argc<3)
     {
         printf("Usage: clientApp servIP servPortNum\n");
     }

     servIP = argv[1];
     servPortNum = atoi(*argv[2]);


}

15:9: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’

#include <stdio.h>


int main(int argc, char *argv[])
{

     char servIP[3];
     int servPortNum;
     if(argc<3)
     {
         printf("Usage: clientApp servIP servPortNum\n");
     }

     servIP = argv[1];
     servPortNum = atoi(*argv[2]);


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

许一世地老天荒 2025-01-05 06:53:21
strncpy (servIP, argv [1], sizeof (servIP) - 1);
servIP [sizeof (servIP) - 1] = 0;

但您确定 servIP 对于 IP 地址来说足够大吗?

strncpy (servIP, argv [1], sizeof (servIP) - 1);
servIP [sizeof (servIP) - 1] = 0;

But are you sure servIP is big enough for an IP address?

谁的新欢旧爱 2025-01-05 06:53:21

您不能分配给数组。使用 strcpystrncpy 函数将字符串复制到 char 数组中。

You cannot assign to arrays. Use strcpy or strncpy function to copy a string in an array of char.

淡莣 2025-01-05 06:53:21

servIP 是一个数组,而不是指针。数组可以转换为指针,但它们不是同一件事,指针不会转换为数组。

servIP is an array, not a pointer. Arrays convert to pointers, but they aren't the same thing and pointers don't convert to arrays.

奶气 2025-01-05 06:53:21

你不能像这样分配一个数组。逐个成员分配它,或使用 char *servIP 代替。

You can't assign an array like this. Assign it member-by-member, or use char *servIP instead.

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