我有pcap库的工作,sniff网络流量在c++中。在pcap库中,pcap_if_t是一个struct,如下所示:
struct pcap_if {
struct pcap_if *next;
char *name; /* name to hand to "pcap_open_live()" */
char *description; /* textual description of interface, or NULL */
struct pcap_addr *addresses;
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
};
typedef struct pcap_if pcap_if_t;现在,我对在&alldevs原型中使用int pcap_findalldevs(pcap_if_t **, char *);值作为下面代码中的第一个值感到困惑。而pcap_if_t *alldevs是一个指针,与pcap_if_t **参数不匹配(该参数是引用另一个指针的指针)。
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list from the local machine */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}例如,我甚至用int **arr语句测试“指针指针”(指针链)。我将int **arr定义为matrix:
int **arr = new int*[5];
for(int i = 0; i < 5; i++)
{
arr[i] = new int[3];
}然后定义void print_arr(int **arr, int r, int c)函数,因为打印了matrix值,并给出了两个不同的参数(arr和&arr2):
int *arr2 = static_cast<int*>(*arr);
print_arr(arr, 5, 3); // arr defined in top
print_arr(&arr2, 5, 4); // arr2 is a pointer as *arr2不管提供了什么示例,c++如何处理此转换?这几个模棱两可的认识。
发布于 2017-05-01 19:24:08
此上下文中的&运算符是address of运算符。当您获取一个变量的地址时,您将得到一个指向该变量的指针。指针存储地址。
例如:
int a = 1;
int *b = &a;b现在指向您的a变量。由于a的类型为int,&a为您提供了指向int的指针类型,即int*。
如果你再做一次,也会发生同样的事情:
int a = 1;
int *b = &a;
int **c = &b;现在,c指向b。由于b具有int *类型,所以&b为您提供指向int指针( int ** )的类型指针。
在函数调用中也会发生同样的情况。
if (pcap_findalldevs(&alldevs, errbuf) == -1)您可以获取alldevs变量的地址。因为alldevs的类型是pcap_if_t*,&alldevs的类型是pcap_if_t**。
你也可以这样做:
pcap_if_t *alldevs;
pcap_if_t **alldevs_ptr = &alldevs;
if (pcap_findalldevs(alldevs_ptr, errbuf) == -1)发布于 2017-05-02 12:22:19
我想用下面的代码提供一个很好的例子:
int f = 48;
int *g = &f;
int **h = &g;
int ***i = &h;
int ****j = &i;
cout << j << endl;
cout << &i << endl;在这里,j是一个包含i地址的链式指针,因此输出如下所示:
0x7ffe373a6220
0x7ffe373a6220两个结果显示了j值(地址为pointed to)和i内存地址,这两个地址是相同的,因为j包含&i。h和&g为:
cout << h << endl;
cout << &g << endl;此外,输出显示了h值(指向g地址)和&g,它们在这里是相同的:
0x7ffe373a6210
0x7ffe373a6210https://stackoverflow.com/questions/43724974
复制相似问题