cannot convert ‘char**’ to ‘char*’

一曲冷凌霜 提交于 2019-12-12 04:04:25

问题


I have an index for the network interface I got a packet from (i.e. 2), and need to find the name of interface, which should return "eth0". I'm using if_indextoname().

I'm not much familiar with C++ on Ubuntu, but my code drops an error:

cannot convert char** to char* for argument 2 to 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;
}

回答1:


char *ifname[10]; declares 10 char pointers.

I guess what you need is a char pointer.
char* ifname = new char[IF_NAMESIZE+1] should solve your problem.

Alternatively, you could just allocate an auto char buffer, if you do not want to pass it to other functions.

char ifname[IF_NAMESIZE+1]



来源:https://stackoverflow.com/questions/24108811/cannot-convert-char-to-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!