0

When a retrieve info from my db fetch_row(result) I want to select from these results and store them in a dynamic array row[i] will be the info a need

I'll need to store it to tagid[trigger]

but char* can be stored to char

so what i now is tagid[trigger] = *row[i];

but when i check the results... it aint what i want the number 358713020035990 needs to be in tagid...

row[i]  0x05df2090 "358713020035990"    char *

tagid[i]    -112 '' char

how do i get this right?

char *tagid;int trigger;
tagid = (char *) malloc(sizeof(char));
result = mysql_store_result(conn); // only one column of integers
num_rows = mysql_num_rows(result);
while (row = mysql_fetch_row(result))
{tagid[trigger] = *row[i];}

1 Answers1

0

If you are trying to copy a string data buffer, and not just the pointer to that buffer, then you are going to have to use a memory copy operation or preferably a standard library function made for such purposes like strcpy, or strncpy. So assuming that tagid[trigger] is referring to a block of memory that is an array of type char, you could do the following:

#include <string.h>

//tagid is a two-dimensional array of chars of ROWSIZE x COLUMNSIZE
char** tagid;
tagid = malloc(sizeof(char*) * COLUMNSIZE);
for (int i=0; i < COLUMNSIZE; i++)
{
    tagid[i] = malloc(sizeof(char) * ROWSIZE);
}

//copy some data into your array at row index "trigger"
int trigger = SOMEVALUE;
strncpy(tagid[trigger], row[i], ROWSIZE);

//free the memory you've allocated for your two dimensional array
for (int i=0; i < COLUMNSIZE; i++)
{
    free(tagid[i]);
}
free(tagid);

The value of ROWSIZE will have to be big enough to hold your largest string plus a terminating NULL otherwise the copy will be truncated using strncpy, or the data will overflow the array bounds and will write-over something else you don't want it to if you use strcpy.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Okay, I see you added some additional code, so I've updated my answer. Note how I've allocated my array as a two-dimensional array, and how I free the memory I've allocated when I'm done. This is quite a bit different than how you've done it. – Jason Jun 19 '11 at 22:24