无法将"char **"转换为"char *" [英] cannot convert ‘char**’ to ‘char*’

查看:330
本文介绍了无法将"char **"转换为"char *"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络接口的索引,该网络接口是从(即2)获得数据包的,需要找到接口名称,该名称应返回"eth0".我正在使用 if_indextoname() .

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().

我对Ubuntu上的C ++不太熟悉,但是我的代码删除了一个错误:

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

无法将参数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];声明10个char指针.

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

我猜您需要的是一个char指针.
char* ifname = new char[IF_NAMESIZE+1]应该可以解决您的问题.

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

或者,如果您不想将其传递给其他函数,则可以分配一个自动char缓冲区.

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]

这篇关于无法将"char **"转换为"char *"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆