c编程出错合集

1.类型冲突

错误:与‘getmyip’类型冲突

 static char *getmyip(char *intf_name)

以为是返回值有问题,东看西看。发现是函数没有声明。

定义或声明应该在使用函数之前

2.段错误

数组没有初始化

inet_ntop将数值格式(addrptr)转换到表达式(strptr)。inet_ntop函数的strptr参数不可以是一个空指针。

inet_ntop(AF_INET,(struct sockaddr_in *)&addr->sin_addr,ipstring,INET_ADDRSTRLEN);函数声明

char *ipstring;

ipstring = (char *)malloc(INET_ADDRSTRLEN);

或者

char ipstring[INET_ADDRSTRLEN];

如果直接char *ipstring;则会出现段错误。

snprintf等函数使用时,参数和占位符匹配,否则发生段错误。

猜你喜欢

转载自blog.csdn.net/maryfei/article/details/80290030