I'm passing a po开发者_C百科inter to a pointer (**resultSet) to my MySQL function.
Here's an excerpt on how I copy the MySQL data from within the function:
int
getItems(char * cmd, char **resultSet)
{
...
MYSQL initialisations and set-up
...
resultSet = malloc(sizeof(char)*(int)mysql_num_rows);
while((row = mysql_fetch_row(result)))
{
for (i=0 ; i < mysql_num_fields(result); i++)
{
printf("%i: \t", i);
resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);
strcpy(resultSet[counter], row[i]);
printf("%s\n", resultSet[counter]);
}
printf("---------------------\n");
counter++;
}
...
MYSQL cleaning up
...
return 0;
}
Calling it in main with
getItems(cmd, resultSet);
From within my getItems function this
printf("%s\n", resultSet[0]);
seems to work.
However, if I try to access it from outside my function I get a segmentation fault. Why is this?
You probably want:
resultSet = malloc(sizeof(void *)*(int)mysql_num_rows);
Instead of:
resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
as you need pointers not bytes.
If you want to use resultSet as a return parameter, you need to make the function signature
int getItems(char * cmd, char ***resultSet)
and use it in the function like
*resultSet = malloc(sizeof(char)*(int)mysql_num_rows)
The function call could look like
char** results;
nitems = getItems(somecmd, &results);
Better and simpler is probably to leave it as it is and make the allocation before the function call.
Two problems here:
- As indicated the allocation of resultset is wrong.
- Secondly, strlen(row[i]+1) will calculate the string length of the memory location referenced by row[i]+1. This length will be 1 less then the string length of row[i]. Since you're basically duplicating a null terminated string, use that function:
resultSet[counter] = strdup((char *)row[i]);
No need to malloc and strcpy.
As far as I can see, you have an array of char arrays that you want to fill with your data. Your initial allocation should be changed to:
resultSet = malloc(sizeof(char*)*(int)mysql_num_rows);
To reflect this (notice char*).
Also:
resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);
This row is technically correct, but you should change it to:
resultSet[counter] = malloc(sizeof(char) * (strlen(row[i])+1) );
To reflect what you actually want to do (the reason for this is the order that C performs arithmetics, and the first approach would yield wrong results if you tried to do it on any other datatype than char/unsigned char).
Finally, I expect that you want to return the value of the 2d-array somehow. There are two ways to do this:
- Make the function return a char** (return NULL on failure). You don't need resultSet as an input parameter here.
- int getItems(char * cmd, char ***resultSet)
Notice that everything with resultSet in the function would have to change to *resultSet. The function can then be called with:
char **result;
int status = getItems(cmd, &result);
You probably just need to set the return value (or the parameter) to &resultSet. But we could know for sure if you showed the parameters to the function and the calling of the function.
精彩评论