gorexy
asked on
1D array to 2D array
Hi,
I am using SQLite but I face a problem
The example retrieve the data form SQLite by
ret = sqlite_get_table(db, sql, &result, &rows, &cols, &errmsg);
the result will be stored in result which was declared as
char **result;
to print the data we use
for (i=0; i< cols * (rows+1); i++) {
printf("result[%d] = %s\n", i, result[i]);
}
but that is 1D array
how can I change this into 2D array and use the convention
for (i=0; i<rows-1; i++)
for (j=0; j<cols-1; j++)
printf("result[%d][%d] = %s\n", i,j, result[i][j]);
how?
I am using SQLite but I face a problem
The example retrieve the data form SQLite by
ret = sqlite_get_table(db, sql, &result, &rows, &cols, &errmsg);
the result will be stored in result which was declared as
char **result;
to print the data we use
for (i=0; i< cols * (rows+1); i++) {
printf("result[%d] = %s\n", i, result[i]);
}
but that is 1D array
how can I change this into 2D array and use the convention
for (i=0; i<rows-1; i++)
for (j=0; j<cols-1; j++)
printf("result[%d][%d] = %s\n", i,j, result[i][j]);
how?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry in fact what char **result means? I found hard to control the double pointer
char ** result means a pointer to a char pointer. Its a way of implementing variable-dimension arrays and passing multiple return values from functions.
If you want to return a success/failure return code from a function AND return other data you generally pass a pointer to that data. If that data is a char pointer then you pass a pointer to it. Since this is a pointer to a string or data buffer then a char** is merely a pointer to a string pointer.
Controlling them syntactically usually involves something like:
... = (*result)[i];
but you will often find it easier to read and therefore maintain if you use something like:
char * str = *result;
... = str[i];
Paul
If you want to return a success/failure return code from a function AND return other data you generally pass a pointer to that data. If that data is a char pointer then you pass a pointer to it. Since this is a pointer to a string or data buffer then a char** is merely a pointer to a string pointer.
Controlling them syntactically usually involves something like:
... = (*result)[i];
but you will often find it easier to read and therefore maintain if you use something like:
char * str = *result;
... = str[i];
Paul
ASKER
Thanks for your information.
Sometimes I see char ***result.....I feel very strange, is it necessary to use trible pointers.
Sometimes I see char ***result.....I feel very strange, is it necessary to use trible pointers.
>>is it necessary to use trible pointers.
Sometimes, but very rarely.
Paul
Sometimes, but very rarely.
Paul
ASKER
#include <stdio.h>
#include <sqlite.h>
int callback(void *pArg, int argc, char **argv, char **columnNames)
{
int i;
for (i=0; i<argc; i++) {
printf("%-10s %-8s %s\n",
columnNames[i], columnNames[i+argc], argv[i]);
}
return(0);
}
main()
{
char *errmsg;
int ret;
sqlite *db = sqlite_open("progdb", 0777, &errmsg);
if (db == 0)
{
fprintf(stderr, "Could not open database: %s\n", errmsg);
sqlite_freemem(errmsg);
exit(1);
}
ret = sqlite_exec(db, "PRAGMA SHOW_DATATYPES=ON", NULL, NULL, NULL);
ret = sqlite_exec(db, "SELECT * FROM contacts", callback, NULL, &errmsg);
if (ret != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
}
sqlite_close(db);
}
If you have time, can you have a look of this?
Is there any way that we can combine the callback function with the main? I get very confused with the **
I can post a new question if necessary
#include <sqlite.h>
int callback(void *pArg, int argc, char **argv, char **columnNames)
{
int i;
for (i=0; i<argc; i++) {
printf("%-10s %-8s %s\n",
columnNames[i], columnNames[i+argc], argv[i]);
}
return(0);
}
main()
{
char *errmsg;
int ret;
sqlite *db = sqlite_open("progdb", 0777, &errmsg);
if (db == 0)
{
fprintf(stderr, "Could not open database: %s\n", errmsg);
sqlite_freemem(errmsg);
exit(1);
}
ret = sqlite_exec(db, "PRAGMA SHOW_DATATYPES=ON", NULL, NULL, NULL);
ret = sqlite_exec(db, "SELECT * FROM contacts", callback, NULL, &errmsg);
if (ret != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
}
sqlite_close(db);
}
If you have time, can you have a look of this?
Is there any way that we can combine the callback function with the main? I get very confused with the **
I can post a new question if necessary
ASKER
thanks