Search code examples
c++ifconfig

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


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;
}

Solution

  • 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]