回调函数有提取双指针的问题

时间:2012-06-29 22:14:11

标签: objective-c c cocoa-touch sqlite

// issue with the following not returning info or just stalling... but no errors
static int callback(void *voidParam, int argc, char **argv, char **azColName)
{   
    static int k = 0;
    myReturn structReturn;

    char *cTempF = (argv[0] ? argv[0] : "NULL");
    memcpy(&structReturn.cFirstName, cTempF, sizeof(cTempF));// tried strlen(cTempF)... no help

    memcpy(voidParam + k * sizeof(structReturn), &structReturn, sizeof(structReturn));

    k++;
}

myReturn *structReturn = (myReturn*)malloc(sizeof(myReturn) * 20);

rc = sqlite3_exec(db, pSQL, callback, structReturn, &zErrMsg);

typedef struct ReturnTag
{
    char cFirstName[100];
    char cLastName[100];
    short int iAge;
}myReturn;

1 个答案:

答案 0 :(得分:1)

解决了......需要:memset(&structReturn.cFirstName, '\0', 100);

static int callback(void *voidParam, int argc, char **argv, char **azColName)
{   
    static int k = 0;
    myReturn structReturn;

    char *cTempF = (argv[0] ? argv[0] : "NULL");
    memset(&structReturn.cFirstName, '\0', 100); // <--- needed here
    memcpy(&structReturn.cFirstName, cTempF, strlen(cTempF));

    memcpy(voidParam + k * sizeof(structReturn), &structReturn, sizeof(structReturn));

    k++;
}