[英]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. 我试图将一个指向数组的指针传递给一个sqlite3操作的回调函数。 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, ¶mStruct, &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: 假设数据总是足够大以填充整个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). 否则,请注意尽可能多地复制实际拥有的数据,而不仅仅是sizeof(整个数组)。
Array declared like that is not a pointer, it's a part of the structure. 声明的数组不是指针,它是结构的一部分。 That structure is at least 8KB long.
该结构至少8KB长。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.