vendredi 24 juin 2016

C pointer to array in struct passed into function

I am trying to pass a pointer to an array, inside a structure into a callback function for an sqlite3 operation. Unfortunately the logic stemming from my understanding of pointers (obviously missing something) does not work.

Simplified, this is what I have:

    typedef struct sqlCallbackData
    {
        int16_t data_array[128][32];

        // There are other members here too...

    } SqlCallbackData_t;

    //===========================================================================================================
    //===========================================================================================================

    void sql_getData(uint8_t seq_num, uint8_t seq_bank, int16_t *data, char *name)
    {
        // 'data' above will be a pointer to a 2D array

        //...

        // Initialise struct members to be passed into callback...
        SqlCallbackData_t paramStruct;

        //Thows: error: incompatible types when assigning to type 'int16_t[128][32]' from type 'int16_t *'
        paramStruct.data_array = data;// I know this isn't right...

        //...

        // Pointer to paramStruct passed into void pointer in callback...
        if (sqlite3_exec(dbPtr, sql, callback, &paramStruct, &zErrMsg) != SQLITE_OK)
        {
            fprintf (stderr, "SQL error: %srn", zErrMsg);
            sqlite3_free(zErrMsg);
        }

        //...
    }

    //===========================================================================================================
    //===========================================================================================================
    // Callback function

    static int load_callback(void *paramStruct, int argc, char **argv, char **azColName)
    {
        int i;

        uint8_t value;

        //...

        // Just making the syntax below a bit mroe readable, as I access members a fair bit in this function
        SqlCallbackData_t *params = ((SqlCallbackData_t *)paramStruct);

        //...
        // Data retreived from sql database...

        params->data_array[0][0] = value;// <- What I'm trying to acheive...obviosuly not correct

        return 0;
    }

So I am aware of how pointers to array are passed into functions (explained here ), but I am getting confused as to how i assign this pointer to array into a structure, to be passed into a function (and then be accessed as an array again).

Going round in circles, so any help would be greatly appreciated:)

Aucun commentaire:

Enregistrer un commentaire