3

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.

3
  • What does the compiler tell you? Commented Feb 9, 2009 at 17:41
  • mdat[r][0] = ptr_f; //illegal index, indirection not allowed. However when this was in main, was not an issue. (//pointers returned from malloc- char * ptr_f;) Commented Feb 9, 2009 at 17:44
  • 1. drop & before myData and tableData. 2. mdat should be char***, not char**. Commented Feb 9, 2009 at 18:10

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Could a cast be missing before the both malloc? It would be like: myData = (char **)malloc(sizeof(char *) * NUM_ROWS); and myData[i] = (char *)malloc(sizeof(char) * NUM_COLS); instead.
3

First of all, the problem with myData is that it's the wrong type. char* [][] would require a prototype char*** (a two-dimensional array of strings) in the function you're calling. The function wants a list of strings, which is char* [], or alternatively char[][], if you don't mind limiting the size of the strings.

To get fully dynamic array sizes you'll have to manually allocate (and release!) memory with malloc() and free(), and change the types of your variables to char **myData and char ***tableData.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.