简单地执行以下操作会导致段错误。 可能是 argv 或 argc 的东西? 真的迷路了。提前致谢!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#define MESSAGE "Not so important message"
int main(int argc, char * argv[])
{
printf(argc);
return 0;
}
最佳答案
if(hp != NULL)
{
printf("Here 6");
perror("Failed to get host by name!");
exit(1);
}
如果 hp!=NULL
为什么要退出?应该反过来。在这个不正确的检查之后,你正在取消引用空指针:
bcopy((char *)hp->h_addr, (char *) &server.sin_addr.s_addr, hp->h_length)
更改为:
if(hp == NULL)
{
perror("Failed to get host by name!");
exit(1);
}
关于c - 客户端程序中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743997/