c++ - when I use a function pointer variables disappear -


ok playing around in spare time trying build function libraries fun, , i'm teaching myself function pointers. tried making binary_search function looks oldest item in array of items. know algorithm works, can't past bug. reason when code entered function pointer stops executing , dies.... did testing , moment code enters strvoidcmp, 2 void *s i'm feeding null...

#include <stdio.h> #include <string.h> #include <math.h>  #include "algorithms.h"  void generate_array(char (*partial_list)[10]){     size_t j = 0, = 0;     for(j = 0; j < 20; j++){         for(i = 0; < 10; i++){               partial_list[i][j] = 0;         }     }       for(i = 0; < 11; i++){         memcpy(partial_list[i], "is there", 9);     }      for(i = 0; < 9; i++){         memcpy(partial_list[i], "not there", 10);     } }  void lmerror(char *msg){     fprintf(stderr, msg);     fflush(stderr); }  int binary_search_top(void **list, const int size, void *value, int (*compare)(void *, void *)){     size_t hi = 0;     size_t lo = 0;     size_t mid = 0;     size_t found = 0;      if(list == null){         lmerror("null found in function binary_search");         return -1;     }      hi = size;     printf("test: %s\n", (char *)value);     while(lo < hi){         mid = ceil((hi - lo) / 2 + lo);             printf("test: %d\n", mid);         if(compare(list[mid], value)){             hi = mid - 1;                 printf("test1\n");         }else{             lo = mid;             found = mid;             printf("test2\n");         }     }     printf("exiting");     return found; }  int strvoidcmp(void *p1, void *p2){     printf("testing: %s, %s\n", (char *)p1, (char *)p2);     return strcmp((char *)p1, (char *)p2); }  int main(){     int (*compare)(void *, void *);      char partial_list[20][10];     const size_t length = 20;     char value[] = "is there";     int ret = 0;     compare = strvoidcmp;     printf("test: %d\n", ret);     generate_array(partial_list);     printf("test: %d\n", ret);     ret = binary_search_top((void **)partial_list, length, value, compare);     printf("test: %d\n", ret);     return 0; } 

your problem partial_list not array of pointers. binary_search_top() gets argument not conform expects.

to verify binary_search_top() works way have implemented it, pass binary_search_top() can use.

void *partial_list_pointers[20];  (i = 0; < length; ++i) {     partial_list_pointers[i] = partial_list[i]; } ret = binary_search_top(partial_list_pointers, length, value, compare); 

with few exceptions, when used in expression, name of array blah decay pointer blah value of address of first element. partial_list, since array 20 , array 10 of char, decay pointer array 10 of char. not pointer pointer.

partial_list_pointers addresses being array 20 of pointer of void. decay pointer pointer of void, binary_search_top() wants in first argument.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -