warning:assignment makes integer from pointer without a cast.

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(const char *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;
}
Last edited on
store and search_term are a pointers to char and store[j] is a char. you are assigning a pointer address to a char value.

also, in your statement char search_term[k+1]; the value in [] should be a constant.
Last edited on
thanks abhiskekm71, ah yes that makes sense.
k remains constant throughout the function, so technically the value in [] is a constant right?

Could you suggest a way to make each pointer in store point to the first character of the string search_term.
maybe you can try:

EDIT: code deleted.

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.
Last edited on
Hmm, i now get an error "lvalue required as left operand of assignment "
for tempPtr++ = search_term;

Sorry i'm a newbie to coding and pointers.
Last edited on
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];
Last edited on
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(const char *query, const int 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>
Last edited on
arent classes only used in c++, i'm required to do this only in C O.o
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(const char *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.
Last edited on
yes all my pointers point to the last search_term, which means all the pointers, except the last one, point to garbage, is there anyway around that?
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.
Last edited on
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
Topic archived. No new replies allowed.