i'm new to developing with c. sure enough there'd come a day i need your help. And I guess this time is now :)
What I am trying to do: I am experimenting with MySQL Api in C. For that i wanted to use a struct to hold my SQLQuery and all it's params, e.g. user data for selection or inserting.
Then with a couple of functions i wanted to add, remove and compile the string to use it in a query.
To use my createSqlQuery Function i'd like to pass a reference to my SQLQuery Structure into it and change it within the function. After that functions ends it should not go out of scope and still be usable in the main function.
Thats where i am currently stucked. I guess my Problem is pretty simple, but after looking and trying at it for hours i'm kinda blind to see the solution. Using this Question: Passing pointers/references to structs into functions i finally happend to have a working code in the way that the structure is still usable after the create Function ends.
But for some reason i cant compile my code further because it cant assign something to my structure.
Error Message:
main.c:127:10: error: request for member ‘addParam’ in something not a structure or union
This happened after adding a second * in the create function to get a pointer to pointer (see other stackoverflow question).
This is what i have so far:
typedef struct sqlQuery sqlQuery;
struct sqlQuery{
char *queryS;
char **params; //array of array of chars to hold the params to replace %? in the query
bool (*addParam)(sqlQuery*, char*);
bool (*compile)(sqlQuery*);
};
int main(){
/* ... */
sqlQuery *testQuery = NULL;
printf("%p\n", &testQuery);
printf("%p\n", testQuery);
createSqlQuery(&testQuery,"SELECT * FROM test WHERE name = '%?'");
printf("%p\n", testQuery);
if (testQuery != NULL) printf("working, testQuery still assigned");
//testQuery->addParam(testQuery, "test");
//freeSqlQuery(testQuery);
exit(EXIT_SUCCESS);
}
bool createSqlQuery(sqlQuery **query, char *queryString){
*query = (struct sqlQuery *) malloc(sizeof(struct sqlQuery)); //get heapspace for sqlQuery Struct
printf("%p\n", *query);
//query->addParam = __sqlQueryAddParam; //link param function pointer
//query->compile = __compileSqlQuery; //link compile function pointer
*query->queryS = queryString; //save pointer to query String
//^^^ this is where the error occurs
return true;
}
I also tried combinations and variations of:
(sqlQuery *)*query->queryS = queryString;
with **, or &...
I hope you get what i am struggling with, i guess it's simple but i really cant seem to figure it out :)
Thanks in advance!