如何将一个字符指针数组作为void *传递,然后转换回字符指针数组?

时间:2015-02-12 19:38:47

标签: c arrays pointers void void-pointers

我将一个字符指针数组传递给sqlite3_exec,它接受​​1个参数并将其显示为void *,但后来我想将其作为字符指针数组访问回调函数。

char *output_params[] = {"one", "two"};
result = sqlite3_exec(db, sql_statement, callback, output_params, &zErrMsg);

....

static int callback(void *param, int argc, char **argv, char **azColName) {
    // How do I access my character array?
    char *output_params[2] = (char **)param;
}

我通过后如何访问它?

1 个答案:

答案 0 :(得分:5)

这对我有用:

int callback(void *param, int argc, char **argv, char **azColName)
{
    const char** p = (const char **)param;
    printf("%s\n", p[0]);
    printf("%s\n", p[1]);
}

这是展示这一概念的简单程序。

#include <stdio.h>

void foo(void* in)
{
    char **p = (char**)in;
    printf("%s\n", p[0]);
    printf("%s\n", p[1]);
}

void main(int argc, char** argv)
{
   char *output_params[] = {"one", "two"};
   foo(output_params);
}