Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

1D array to 2D array

Posted on 2006-05-04
7
Medium Priority
?
226 Views
Last Modified: 2010-04-24
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?
0
Comment
Question by:gorexy
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 120 total points
ID: 16608448
Hi gorexy,

for (i=0; i<rows-1; i++)
   for (j=0; j<cols-1; j++)
      printf("result[%d][%d] = %s\n", i,j, result[(i*cols)+j]);


Paul
0
 

Author Comment

by:gorexy
ID: 16613440
oh.. it is a way
thanks
0
 

Author Comment

by:gorexy
ID: 16613441
sorry in fact what   char **result means?  I found hard to control the double pointer
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16617602
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
0
 

Author Comment

by:gorexy
ID: 16619414
Thanks for your information.

Sometimes I see char ***result.....I feel very strange, is it necessary to use trible pointers.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16620813
>>is it necessary to use trible pointers.
Sometimes, but very rarely.

Paul
0
 

Author Comment

by:gorexy
ID: 16622013
#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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question