gpt4 book ai didi

c - 传递 ‘strncmp’ 的参数 1 从整数生成指针,无需强制转换 C 编程

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

我想比较客户端发送到服务器的一些数据。当我使用 strncmp t 比较我从套接字中读取的内容和我用来比较的字符串时,我遇到了这个错误。谁能告诉我为什么会出现这个错误?

warning: passing argument 1 of ‘strncmp’ makes pointer from integer without a cast

服务器:

void
result(int sockfd)
{
ssize_t n;
char buf[MAXLINE];
int temp;
time_t ticks;
int i;
again:
while ((n =read(sockfd, buf, MAXLINE)> 0))
{
buf[n] = '\0';
printf("Message Recieved:%s\n",buf);
srand (time(NULL));
temp = rand() % 40+1;
printf("Ramdom es %i\n",temp);

if ((strncmp (buf[0],"Axx",1) == 0) || (strncmp (buf[0],"axx",1) == 0))
{
snprintf(buf, sizeof(buf), "Option A choosen in %i on %.24s\r\n", temp,ctime(&ticks));
Writen(sockfd, buf, n);
}
if ((strncmp (buf[0],"Bxx",1) == 0) || (strncmp (buf[0],"bxx",1) == 0))
{
snprintf(buf, sizeof(buf), "Option B choosen in %i on %.24s\r\n", temp,ctime(&ticks));
Writen(sockfd, buf, n);
}
else
{
printf("Incorrect Input");
Close(sockfd);
break;
}
}
if (n < 0 && errno == EINTR)
goto again;
else if (n < 0)
err_sys("read error");
}

int
main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
/*char message[MAXLINE];*/
char recvline[MAXLINE + 1];

listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);/*----------------------------------------------------*/
servaddr.sin_port = htons(5678);

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
printf("EDMTS is running on 127.0.0.1, listening on port 5678\n");
printf("\n");
printf("Waiting for incoming connections...Press Ctrl+C to end server\n");

for ( ; ; )
{
len = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &len);

/*Client connects to server*/
printf("\n");
printf("Connection from %s, port %d\n",
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));

result(connfd);
Close(connfd);

}
}

非常感谢。

最佳答案

strncmp 期望 const char* 作为第一个参数,而您只传递一个 char。

将其更改为:

 if ((strncmp (buf,"Bxx",1) == 0) || (strncmp (buf,"bxx",1) == 0))

关于c - 传递 ‘strncmp’ 的参数 1 从整数生成指针,无需强制转换 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591369/

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