简体   繁体   中英

How do I pass an array of character pointers as void *, then cast back to array of character pointers?

I'm passing an array of character pointers to sqlite3_exec , which takes 1 parameter and presents it as a void * , but then I want to access it as the array of character pointers in the callback function.

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;
}

How do I access it after I pass it?

This works for me:

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]);
}

Here's simple program that demonstrates the concept.

#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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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