[EMAIL PROTECTED] wrote:
sqlite3_open(dbname, &db);
sqlite3_prepare_v2(db, sql, -1, &pStat, 0);
while(sqlite3_step(pStat) == SQLITE_ROW)
{
row = (char *)sqlite3_column_text(pStat, 0);
my_array[i] = malloc( sizeof row * sizeof *my_array[i]);
memcpy (my_array[i], row, sizeof row * sizeof *my_array[i]);
sizeof(row) == sizeof(char*) == 4 (assuming a 32-bit system). In any
case, sizeof(row) is completely unrelated to the length of the string
'row' might be pointing to. See sqlite3_column_bytes.
printf ("%s\n",my_array[i]); // ok
Are you sure the output is correct here? It's only possible if your
strings happen to be short enough to fit in 4 bytes.
i++;
}
for (i = 0; i<3; i++);{
printf ("%s\n", my_array[i]); //error
Remove the semicolon between the closing paren and the opening brace.
Your code as written is equivalent to this:
for (i = 0; i < 3; i++) {
// loop body is empty
}
// At this point, i == 3
printf ("%s\n", my_array[3]); // index out of bounds
free (my_array);
You haven't allocated my_array with malloc, so you shouldn't deallocate
it with free. Your program will likely crash on this statement.
You did allocate my_array[0], my_array[1] and my_array[2] with malloc,
so you should deallocate these three pointers with free. Do you
understand the difference?
With all due respect, I suggest you spend some time with your favorite C
textbook. The problems in your code have very little to do with SQLite,
and a lot to do with misusing C language.
Igor Tandetnik
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------