简体   繁体   English

无法将'char **'转换为'char *'

[英]cannot convert ‘char**’ to ‘char*’

I have an index for the network interface I got a packet from (ie 2), and need to find the name of interface, which should return "eth0" . 我有一个网络接口的索引,该网络接口是我从中获得数据包的(即2),并且需要找到接口名称,该名称应返回"eth0" I'm using if_indextoname() . 我正在使用if_indextoname()

I'm not much familiar with C++ on Ubuntu, but my code drops an error: 我对Ubuntu上的C ++不太熟悉,但是我的代码删除了一个错误:

cannot convert char** to char* for argument 2 to char* if_indextoname(unsigned int, char*) 无法将参数2的char**转换为char*char* if_indextoname(unsigned int, char*)

Can someone help me to fix it? 有人可以帮我解决吗?

#include <net/if.h>
#include <iostream>    

int main()
{
    unsigned int ifindex = 2;
    char *ifname[10];
    std::cout << if_indextoname(ifindex, ifname);
    std::cout << ifname << std::endl;
}

char *ifname[10]; declares 10 char pointers. 声明10个char指针。

I guess what you need is a char pointer. 我猜您需要的是一个char指针。
char* ifname = new char[IF_NAMESIZE+1] should solve your problem. char* ifname = new char[IF_NAMESIZE+1]应该可以解决您的问题。

Alternatively, you could just allocate an auto char buffer, if you do not want to pass it to other functions. 另外,如果您不想将其传递给其他函数,则可以分配一个自动char缓冲区。

char ifname[IF_NAMESIZE+1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM