C - Transmission over a reference multidimensional array with a known size

Mostly:

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

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

and goes into the function:

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

Doesn't it seem like it? Any suggestions? Many examples are online when you do not know the size, having tried them right now.

Thanks.

+3
source share
2 answers

First of all, the problem with myData is that it is the wrong type. char * [] [] will require a prototype char *** (two-dimensional array of strings) in the called function. The function wants a list of strings, which is char * [], or, alternatively, char [] [], if you do not mind limiting the size of the strings.

, ( !) malloc() free() char ** myData char * ** tableData.

+3

:

#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*/
}

newCallBack(), (myData​​strong > , tableData​​strong > ):

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

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

, , vars myData tableData​​strong > , r:

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

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

Source: https://habr.com/ru/post/1702926/


All Articles