使用和取消引用(无效**) [英] Using and dereferencing (void**)

查看:207
本文介绍了使用和取消引用(无效**)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过指针的多态阵列功能。

I would like to pass a "polymorphic" array of pointers to a function.

我可以不用警告如下:

foo (void* ptr);

bar()
{
  int* x;
  ...
  foo(x);
}

GCC显然自动转换 X (无效*),这仅仅是花花公子。

gcc apparently automatically casts x to a (void*), which is just dandy.

不过,我得到一个警告,当我做到以下几点:

However, I get a warning when I do the following:

foo (void** ptr);

bar()
{
  int** x; // an array of pointers to int arrays
  ...
  foo(x);
}

note: expected ‘void **’ but argument is of type ‘int **’
warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]

我的问题是:为什么是传递一个(INT *)(无效*)参数不是'不兼容,但(INT **)(无效**)参数是什么?

My question is: why is passing an (int*) as a (void*) argument not 'incompatible', but (int**) as a (void**) argument is?

由于所有的指针类型的大小相同(右?它是因为我已经用c一段时间),
我还可以这样做:

Since all pointer types are the same size (right? it's been a while since I've used C), I can still do something like:

void mainFunc1(int** arr, int len)
{
    //goal is to apply baz to every int array
    foo(baz, arr, len);
}

void mainFunc2(double** arr, int len)
{
    //goal is to apply baz to every int array
    foo(qux, arr, len);
}

// I PROMISE that if I pass in a (int**) as ptr, then funcPtr will interpret its (void*) argument as an (int*)
void foo(funcPtr f, void** ptr, int len)
{
    for(int i = 0; i < len; i++)
    {
        f(ptr[i]);
    }
}

void baz(void* x) 
{
  int* y = (int*)x;
  ...
}

void qux(void* x)
{
  double* y = (double*)x;
  ...
}

对于所有的空指针的目的是为了让我可以使用应用到会(下叠)有不同类型的PTR参数的函数的函数指针:有些人会采取 INT 阵列,有些会采取双击阵列等

The purpose for all the void pointers is so that I can use a function pointer applied to functions that will (down the stack) have different types of ptr arguments: some will take int arrays, some will take double arrays, etc.

推荐答案

请注意:无效* 是通用的。但无效** 不是。您可以指定任何类型的地址无效* 变量,但无效** 可分配的地址无效* 唯一的变量。

Note: void* is generic. but void** is not. You can assign address of any type to void* variable but void** can be assigned address of void* variable only.

void* generic;
int i;
int *ptri = &i;

generic = ptri;

char c;
int *ptrc = &c;

generic = ptrc;

有效的,但下面是一个错误:

valid but following is an error:

void**  not_generic;
int i;
int *ptri = &i;
int **ptr_to_ptr1 = &ptri;
void**  not_generic = ptr_to_ptr1;

错误:分配 INT ** 无效**

是的,你可以这样做:

void**  not_generic;
not_generic = &genric;

有关通用阵列功能只需使用无效* A 如下:

For generic array function simply use void* a as follows:

enum {INT, CHAR, FLOAT};
void print_array(void* a, int length, int type){
   int i = 0;
   for(i = 0; i < lenght; i++){
      switch(type){
         case INT: 
              printf("%d", *((int*)a + i));
              break;
         case CHAR: 
              printf("%c", *((char*)a + i));
              break;
         case FLOAT: 
              printf("%f", *((float*)a + i));
              break;
      }
   }
}

您更好地使用宏写这个功能。

You better write this function using macros.

调用该函数:

假设INT:

   int a[] = {1, 2, 3, 4}; 
   print_array(a, sizeof(a)/sizeof(a[0]), INT);

假设字符:

   char a[] = {'1', '2', '3', '4'}; 
   print_array(a, sizeof(a)/sizeof(a[0]), CHAR);

这篇关于使用和取消引用(无效**)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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