简体   繁体   English

从 (void**) 转换为 (int*) 反之亦然

[英]Cast from (void**) to (int*) and viceversa

I did a function f which takes as input a (void*), convert it to a (int*) and print the value.我做了一个 function f 作为输入(void *),将其转换为(int *)并打印值。

#include <stdlib.h>
#include <stdio.h>

void    f(void* p)
{
    int *pi = (int*)p;
    printf("%d\n", *pi);
}

int main()
{
    int x = 1;
    int *px = &x;
    void    *pv = (void*)px;
    f(pv);
    
    return 0;
}

Is it possible to implement a function:是否可以实现 function:

void f2(void** pp);

such that it performs the "same" operations of the function f?这样它就可以执行 function f 的“相同”操作? My goal is to learn how to convert a (int*) to a (void**) and viceversa.我的目标是学习如何将 (int*) 转换为 (void**),反之亦然。


EDIT: error and warning of @tadman code (I did a mistake)编辑:@tadman 代码的错误和警告(我做错了)

fvv.c: In function ‘f2’:
fvv.c:10:12: warning: initialization of ‘int *’ from incompatible pointer type ‘int **’ [-Wincompatible-pointer-types]
   10 |  int *pi = (int**)p;
      |            ^
fvv.c:12:17: error: invalid type argument of unary ‘*’ (have ‘int’)
   12 |  printf("%d\n", **pi);
      |   

EDIT2编辑2

fvv.c: In function ‘main’:
fvv.c:19:5: warning: passing argument 1 of ‘f2’ from incompatible pointer type [-Wincompatible-pointer-types]
   19 |  f2(&px);
      |     ^~~
      |     |
      |     int **
fvv.c:7:16: note: expected ‘void **’ but argument is of type ‘int **’
    7 | void f2(void** p)

You can take any level of indirection you want, up to the blatantly, utterly absurd ( void******* ), but I'm not sure why this would be useful:你可以采取任何你想要的间接级别,直到公然,完全荒谬( void******* ),但我不确定为什么这会有用:

void f2(void** p)
{
    // Note you must maintain the same level of indirection
    int **pi = (int**)p;

    // Since this is a ** pointer, it requires ** to fully de-reference
    printf("%d\n", **pi);
}

To call this you need a pointer to a pointer:要调用它,您需要一个指向指针的指针:

int x = 1;
int *px = &x;
f2((void**) &px);

In C terms a pointer to a pointer is often interpreted to mean one of the two following things:在 C 术语中,指向指针的指针通常被解释为以下两种情况之一:

  • A two dimensional array二维数组
  • A mutable pointer argument可变指针参数

Neither of those apply here.这些都不适用于这里。

That being said, in C there's not a lot of rules as to what you can and can't convert.话虽如此,在 C 中,关于您可以转换什么和不能转换什么的规则并不多。 If you want to do it, C isn't going to get in your way, even if the resulting code makes no sense or will crash immediately when executed.如果您想这样做,C 不会妨碍您,即使生成的代码没有意义或在执行时会立即崩溃。

You can convert int* to void** and back again, C won't care, but you should have a really good reason for doing such a thing.您可以将int*转换为void**并再次转换回来,C 不会在意,但您应该有充分的理由这样做。 Normally arbitrary pointers are almost always specified as void* , and as this can be recast into whatever you want, it's sufficient.通常任意指针几乎总是被指定为void* ,因为它可以被重铸成你想要的任何东西,这就足够了。

For example, you can specify void* as an argument when that pointer is actually int** , something you'll see quite often, as in thread_create taking a void* arg argument.例如,当指针实际上是int**时,您可以将void*指定为参数,您会经常看到这种情况,例如thread_create采用void* arg参数。 That's not limited to mere pointers, you can cast to your heart's content.这不仅限于指针,您可以随心所欲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM