你想要一個指針指向char
:
#include <stdio.h>
static void myMethod(void *args)
{
char **ptr = args;
printf("%s\n", ptr[1]);
}
int main(void)
{
void *ptr;
char *args[3];
args[0]="Arg1";
args[1]="Arg2";
args[2]="Arg3";
ptr = args; /* You don't need the & */
myMethod(ptr);
return 0;
}
正如@Eregrith指出,通過細胞數量的功能,以防止越界訪問:
#include <stdio.h>
static void myMethod(void *args, size_t elems)
{
char **ptr = args;
for (size_t i = 0; i < elems; i++)
printf("%s\n", ptr[i]);
}
int main(void)
{
void *ptr;
char *args[3];
args[0]="Arg1";
args[1]="Arg2";
args[2]="Arg3";
ptr = args;
myMethod(ptr, sizeof(args)/sizeof(args[0]));
return 0;
}
只需添加演員? – Crozin