Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Pass by reference multidimensional array with known size

In main:

char *myData[500][9]; //dynamic rows??
char **tableData[500]={NULL};         //dynamic rows??
int r;

newCallBack(db, &myData, &tableData, &r);

and passing into function by:

void newCallBack(sqlite3 *db, char** mdat, char*** tdat, int* r )
{

Doesn't seem to like this? Any suggestions? Lots of examples online when you don't know the size, trying them out right now....

Thanks.

like image 558
T.T.T. Avatar asked Apr 20 '26 03:04

T.T.T.


1 Answers

If you were to rewrite this as such:

#define NUM_ROWS 500;
#define NUM_COLS 9;

char **myData  = NULL;
char  *tableData = NULL;
int    i;
int    r;

myData = malloc(sizeof(char *) * NUM_ROWS);
if (!myData)
    return; /*bad return from malloc*/

tableData = malloc(sizeof(char) * NUM_ROWS);
if (!tableData)
    return; /*bad return from malloc*/

for (i = 0; i < NUM_ROWS; i++)
{
    myData[i] = malloc(sizeof(char) * NUM_COLS);
    if (!myData[i])
        return;  /*bad return from malloc*/
}

You would then call newCallBack() like this if you just wanted access to the data (myData, tableData, and r):

/*prototype*/
void newCallBack(sqlite3 *db, char** mdat, char* tdat, int r);

/*call*/
newCallBack(db, myData, tableData, r);

Or this if you want to be able to modify what the vars myData and tableData point to and the value of r:

/*prototype*/
void newCallBack(sqlite3 *db, char ***mdat, char **tdat, int *r);

/*call*/
newCallBack(db, &myData, &tableData, &r);
like image 159
lillq Avatar answered Apr 22 '26 03:04

lillq