ether_ntoa指针转换问题
我正在尝试使用 ether_ntoa 打印 MAC 地址。当我尝试这样做时
printf("MAC (src): %s\n",ether_ntoa((struct ether_addr *)&eheader->ether_shost));
,我遇到了分段错误,所以我想出了两种不同的方法:
这是代码片段n°1:
struct ether_header *eheader;
char *p;
...
p = ether_ntoa(struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);
我得到的警告是:
赋值使指针来自 整数不带强制转换
,所以我进行强制转换并且......
这是代码片段n°2:
struct ether_header *eheader;
char *p;
...
p = (char *) ether_ntoa((struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);
我得到的警告是:
从整数转换为指针 尺寸不同
如果你看一下手册页,ether_ntoa 是这样定义的,返回一个 char *:
extern char *ether_ntoa (__const struct ether_addr *__addr) __THROW;
所以我不知道我做错了什么。问题是“不是”警告,而是当我尝试打印它时出现的分段错误。
我在 openSUSE 下遇到这个错误(在 ubuntu 中我不需要做这个 *p 技巧),所以如果这里有 openSUSE 专家,我将感谢她的帮助。
非常感谢!
I am trying to print the MAC address by using ether_ntoa. When i try to do
printf("MAC (src): %s\n",ether_ntoa((struct ether_addr *)&eheader->ether_shost));
I get a segmentation fault, so I have come up with two different approaches:
This is the snippet code nº1:
struct ether_header *eheader;
char *p;
...
p = ether_ntoa(struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);
and the warning I obtain is:
assignment makes pointer from
integer without a cast
so I do the cast and ...
This is the snippet code nº2:
struct ether_header *eheader;
char *p;
...
p = (char *) ether_ntoa((struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);
and the warning I obtain is:
cast to pointer from integer of
different size
If you take a look at the man page, ether_ntoa is defined this way, returning a char *:
extern char *ether_ntoa (__const struct ether_addr *__addr) __THROW;
so I don't know what I am doing wrong. The problem is "not" the warning, is the segmentation fault it comes after, when I try to print it.
I am getting this error under openSUSE (in ubuntu I don't need to do this *p trick) so if there is an openSUSE expert here I will appreciate her help.
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此警告:
...并不是添加演员的邀请。它告诉你一个更严重的问题;在这种情况下,编译器认为
ether_ntoa()
返回一个int
。当您使用作用域内没有声明的函数时,就会发生这种情况;对于这样的库函数,这意味着您没有包含正确的标头。对于
ether_ntoa()
,您需要#include
。This warning:
...is not an invitation to add a cast. It's telling you about a more serious problem; in this case, that the compiler thinks
ether_ntoa()
returns anint
.This happens when you use a function without a declaration in scope; for a library function like this, it means that you haven't included the right header. For
ether_ntoa()
, you need#include <netinet/ether.h>
.这听起来确实像是标头/库和编译器之间的字大小不匹配。您的 Suse 机器上是否有一些奇怪的 64 位和 32 位环境混合?
That sure sounds like a word-size mismatch between the headers/library and the compiler. Do you have some strange mix of 64-bit and 32-bit environment in that Suse machine?
这是一个图书馆问题。我不知道我怎么没有检查这一点(也许是因为这是老师提供的文件,其中包含所需的所有库)。
执行 include:
是解决方案,所以我认为问题已得到解答。
非常感谢你们俩。
It was a library problem. I don't know how I didn't check that (maybe because it was a file provided by the teacher with all the libraries needed ¬¬).
Doing the include:
was the solution, so I consider the question answered.
Thank you very much to both of you.