I’m trying adapt the example code shown in the “Array Interface Example” section here,
http://orclib.sourceforge.net/doc/html/group_g_bind.html
where they place an array of strings, tab_str, into OCI_BindArrayOfStrings using:
char tab_str[1000][21];
...
OCI_BindArrayOfStrings(st, ":s", (char*) tab_str, 20, 0);
The problem is, the above example knows the array length at compile time, whereas I have to download this length from a database when the program is run. So I’d like to create an array of strings, called my_tab_str and place it in the following line of code:
OCI_BindArrayOfStrings(st, ":s", (char*) my_tab_str, 20, 0);
My question is how to set up my_tab_str? Here’s my code (compiled using gcc -std=C89):
int i, arraysize;
char person_name[20] = "";
char * my_tab_str;
...
strncpy(person_name, "John Smith", 19);
arraysize = <this value is downloaded from database>;
...
my_tab_str = malloc( arraysize * sizeof(char) * (strlen(person_name)+1) );
for(i=0;i<arraysize;i++) {
strncpy( my_tab_str[i], person_name, strlen(person_name) );
}
The goal is to place “John Smith” (e.g 10 bytes) plus a null termination character (which I think is automatically added by the compiler) into each element of the array of strings my_tab_str.
I’m getting the compile warning: warning: passing argument 1 of 'strncpy' makes pointer from integer without a cast
/usr/include/string.h:131: note: expected 'char * __restrict__' but argument is of type 'char'
Note that the function OCI_BindArrayOfStrings is described here:
http://orclib.sourceforge.net/doc/html/group_g_bind.html#ga502cd4785691b17955f5d99276e48884
and expects an array of string as an argument. See the example code at the first link above for an example implementation.
It’s not entirely clear from your post what that function expects as an argument. I’m going to assume it’s achar **.In that case, you need to do something like this:
UPDATE
Ok, so it looks like that function takes a
char *which points to a contiguous 1D array of all the strings concatenated, along with the number of strings, and the length of each string. In which case, you’ll need to do something like this:This is just a guess though, because that function really isn’t well-documented.
Obviously, you will also need some cleanup code at some point that carefully
frees everything.And if you want to be really careful, you should add error-handling code that checks the result of each
callocforNULL, but that would clutter the example, so I’ve omitted it.