1

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: %s\r\n", 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:)

Community
  • 1
  • 1
cSmout
  • 63
  • 2
  • 12

2 Answers2

0

I am trying to pass a pointer to an array, inside a structure

for that you need to change struct like this,look at two dimensional array pointer

typedef struct sqlCallbackData
{
     int16_t (*data_array)[32]; //2d array pointer

    // There are other members here too...

} SqlCallbackData_t;

When you assign address to pointer...

void sql_getData(uint8_t seq_num, uint8_t seq_bank, int16_t (*data)[32], char *name)
    {
        // 'data' above will be a pointer to a 2D array
        // Initialise struct members to be passed into callback...
        SqlCallbackData_t paramStruct;

        paramStruct.data_array = data;
Community
  • 1
  • 1
Mohan
  • 1,871
  • 21
  • 34
  • And this still allows access to the two dimensions of the array? i.e how is this specifying the length of the first dimension? Please excuse if this question seems silly... and thanks for the prompt response – cSmout Jun 17 '16 at 11:02
  • This compiles, but does throw a warning: warning: assignment from incompatible pointer type [enabled by default] – cSmout Jun 17 '16 at 11:05
  • For which line you are getting warning? – Mohan Jun 17 '16 at 11:46
  • No more warnings, your edit did indeed work. Thankyou. I Still need to go away and research how this solution works, as I'm not understanding the way (*data)[32] can still point to two dimensions. – cSmout Jun 17 '16 at 11:54
0

Assuming the data is always large enough to fill the whole data_array:

memcpy(paramStruct.data_array, data, sizeof(paramStruct.data_array))

Otherwise, take care to copy as much data as you actually have instead of just sizeof(whole array).

Array declared like that is not a pointer, it's a part of the structure. That structure is at least 8KB long.

arsv
  • 1,176
  • 9
  • 11
  • I did try something like: `typedef struct sqlCallbackData { int16_t data_array[128][32]; // There are other members here too... } SqlCallbackData_t;` But this still did not work – cSmout Jun 17 '16 at 11:07
  • That does not change the structure itself. You should either have struct sqlCallbackData { int16_t* data_array_ptr; ... } and pass the pointer there, or leave it as { int16_t data_array[128][32]; ... } and copy the data. Check something like http://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array on how to deal with data_array_ptr later. – arsv Jun 17 '16 at 11:15
  • Thanks @arsv, I need the data to be a pointer so that data read out of my db file can be accessed outside of the callback. – cSmout Jun 17 '16 at 11:31