I'm trying to make the function *searchterm_generator return an array of pointers to strings but
I get the warning at store[j] = query_term (where i try to make each pointer in store point to the first character of the string search_term)
I didn't find any answer to my problem and hence had to post it up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
char
*searchterm_generator(constchar *query, int k){
int query_len = strlen(query);
assert(k <= query_len);
int n = k;
char search_term[k+1]; /* +1 to store null char */
char *store = malloc(sizeof(char)*k*query_len);
int record_number = 0; int i; int j;
for(j = 0;j<n;j++){
for (i = 0; i<n ; i++){
search_term[i] = query[i];
if(i == (n-1)){
search_term[k] = '\0';
}
}
store[j] = search_term;
}
return store;
}
Also, if k does not change, you should declare it as a const in your function prototype. Even after that i don't think its valid to assign an array of k or k+1 length the way its done in your code.
The code is not working because store is an array of chars and not pointers. Please tell me what you want to do and only then a suitable algorithm can be suggested.
EDIT: I think, what you want is maybe store[j] = search_term[0];
I want my function to return an array of pointers, each pointing to a whole string, so store[0] can be a string of length 4 and everytime as j changes, the next pointer in store can hold the same (or different string if i tweak my code a little), but the general idea is that i want to return many strings from this function.
the best way would be to declare a vector of std::string types. something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
std::vector<std::string> searchterm_generator(constchar *query, constint k){
int query_len = strlen(query);
assert(k <= query_len);
int n = k;
std::vector<std::string> store;
std::string search_term;
// char *store = malloc(sizeof(char)*k*query_len);
int record_number = 0; int i; int j;
for (j=0; j<n; j++) {
for (i = 0; i<n ; i++)
search_term[i] = query[i]; // no need for NULL character
store.push_back(search_term);
}
return store;
}
ofcourse you will have to #include <string> and <vector>
well i'm not too confident about C but you can try the following. However, please re-check the syntax, and more importantly memory allocation/deallocation scope issues.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
char **searchterm_generator(constchar *query, int k){
int query_len = strlen(query);
assert(k <= query_len);
int n = k;
char search_term[k+1]; /* +1 to store null char */
char **store = (char**) malloc (sizeof(char*)*k); // pointer to pointer of char
int record_number = 0; int i; int j;
for(j = 0;j<n;j++) {
for (i = 0; i<n ; i++){
search_term[i] = query[i];
if(i == (n-1)){
search_term[k] = '\0';
}
}
*store = search_term;
store++;
}
return store;
}
EDIT: there is one flaw, search_term will go out of scope once the function ends and so all the elements in the store array will point to garbage.
you can pass search_item as a parameter to the function. That way, value pointed by search_item shall belong to the calling function and will not go out of scope.
Also, at least in your present function, search_term does not seem to have any use. You could as well assign the *store pointer, the value of query. Note that value pointed by query address does not go out of scope.
what i ended up doing was add all the strings to char arrays in a struct using store as an array of pointers to structs. But thanks for your input abhishekm71, appreciated :D