0

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;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Tina J
  • 4,983
  • 13
  • 59
  • 125
  • 5
    You're passing an array of `char *` to something that expects a `char *`. Maybe you meant `char ifname[IF_NAMESIZE];` (or a higher size, or use the buffer of a `std::string` of that size in C++11). – chris Jun 08 '14 at 17:28
  • 5
    It has nothing to do with Linux or Ubuntu. It is just C++. An array of `char*` cannot decay to a `char *`. – juanchopanza Jun 08 '14 at 17:30

1 Answers1

2

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]

cppcoder
  • 22,227
  • 6
  • 56
  • 81