简体   繁体   中英

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: %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:)

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;

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.

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