c - Pointers On Pointers -
#include <stdio.h> #include <string.h> void f1(void *comp, void *record){ int complen = strlen((char *)comp), recordlen = *(int *)record; *(int *)record = complen>recordlen ? complen : recordlen; } void f2(void *comp, void *ans){ if(!*(char **)ans) *(char **)ans = (char *)comp; else if(strcmp((char *)comp, *(char **)ans) < 0) *(char **)ans = (char *)comp; } void processstrings(char ***vals, void* (*fp)(char *, void *), void *champ){ char **copy = *vals; while(*copy){ fp(*copy++, champ); } } int main() { char *strings1[][100] = {{"beta", "alpha", "gamma", "delta", null}, {"johnson", "smith", "smithson", "zimmerman", "jones", null}, {"mary", "bill", "bob", "zoe", "annabelle", "bobby", "anna", null}}; int maxlen = 0; char *minstring = null; processstrings(strings1, f1, &maxlen); processstrings(strings1, f2, &minstring); printf("strings1: max length %d , min %s\n", maxlen, minstring); } a quick background...function f1 supplies max length of list of strings it's second parameter. f2 supplies minimum string in terms of ascii value.
my error message states i'm passing incompatible pointer type process strings. when draw out pointers, feel though not. help?
you passing incompatible pointer type indeed.
strings1 2d array of pointers chars. note, 2d array elements in c laid out sequentially row after row, while function expects see pointer after first dereference of strings1 inside processstrings.
if want code work correctly, need either pass following construct processstrings
char **strings2[] = { strings1[0], strings1[1], strings2[2] }; or change the function work pointer array of 100 char pointers:
void processstrings2(char * (*vals)[100], void (*fp)(void *, void *), void *champ){ btw, function seems process first row of strings.
Comments
Post a Comment