类型转换简单问题

suisuibianbian 2004-03-24 02:12:27
unsigned long inet_addr (
const char FAR * cp
);
char chrIp[]="192.168.0.1";
unsigned long Ip;
Ip = inet_addr(chrIp);//这句老出错,怎么写
...全文
28 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2004-03-24
1.你定义的inet_addr与库里的原形不配(C++要求完全匹配的,如test(int)与test(char)是
两个不同的函数)
2.inet_addr在ws2_32.lib中,要把库文件加入项目,否则连接通不过
回复
suisuibianbian 2004-03-24
to keiy:
通过了,能告诉我原因吗
回复
sevencat 2004-03-24
再不行的话,用这个吧。
extern int inet_aton(char const * cp, struct in_addr * addr)
{
unsigned int val;
int base, n;
char c;
unsigned int parts[4];
unsigned int *pp = parts;

c = *cp;
for (;;)
{
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit((int)c))
return (0);
val = 0;
base = 10;
if (c == '0')
{
c = *++cp;
if (c == 'x' || c == 'X')
base = 16, c = *++cp;
else
base = 8;
}
for (;;)
{
if (isascii((int)c) && isdigit((int)c))
{
val = (val * base) + (c - '0');
c = *++cp;
}
else if (base == 16 && isascii((int)c) && isxdigit((int)c))
{
val = (val << 4) |
(c + 10 - (islower((int)c) ? 'a' : 'A'));
c = *++cp;
}
else
break;
}
if (c == '.')
{
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3)
return (0);
*pp++ = val;
c = *++cp;
}
else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && (!isascii((int)c) || !isspace((int)c)))
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
switch (n)
{

case 0:
return (0); /* initial nondigit */

case 1: /* a -- 32 bits */
break;

case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;

case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;

case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (addr)
addr->s_addr = htonl(val);
return (1);
}
回复
柯本 2004-03-24
命令行方式:
#include <stdio.h>
#include <winsock2.h>
int main(int argc, char* argv[])
{
char chrIp[]="192.168.0.1";
unsigned long Ip;
Ip = inet_addr(chrIp);
printf("%d\n",Ip);

return 0;
}
1.vc 6.0
cl testm.cpp ws2_32.lib
2.dev-cpp
g++ -o testm testm.cpp -lws2_32
3.BCB 6.0
bcc32 testm.cpp
全部通过!
回复
grooving 2004-03-24
出什么错?

#include <winsock2.h>


ref ws32_2.dll
回复
柯本 2004-03-24
全部程序如下:
// testm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment( lib, "ws2_32" )



int main(int argc, char* argv[])
{
char chrIp[]="192.168.0.1";
unsigned long Ip;
Ip = inet_addr(chrIp);//这句老出错,怎么写
printf("%d\n",Ip);

return 0;
}
回复
suisuibianbian 2004-03-24
怎么把ws2_32.lib加入项目
回复
sevencat 2004-03-24
在#include <windows.h>前面加上
#define WIN32_LEAN_AND_MEAN
然后再
#include <winsock2.h>
回复
柯本 2004-03-24
我重试了下:VC下正确的定义是
extern "C" __declspec(dllimport) unsigned long _stdcall inet_addr (
const char FAR * cp
);
你还是用
#include <winsock2.h>
的好

还有要把ws2_32.lib加入项目
回复
suisuibianbian 2004-03-24
出现很多error C2011:

error C2011: 'fd_set' : 'struct' type redefinition
回复
gracian 2004-03-24
弱弱的回答,能不能把
char chrIp[]="192.168.0.1";
unsigned long Ip;
顺序换一下试试看呢.
回复
柯本 2004-03-24

#include <winsock2.h>

extern "C" unsigned long __stdcall inet_addr (
const char FAR * cp
);
编译就通过
回复
suisuibianbian 2004-03-24
vc6.0
回复
柯本 2004-03-24
你用什么编译器?
回复
发动态
发帖子
C语言
创建于2007-09-28

6.3w+

社区成员

C语言相关问题讨论
申请成为版主
社区公告
暂无公告