Reputation: 9663
I'm trying to call a function hfun
using a pointer to the function that is held inside a struct.
These are the type definitions:
typedef struct Table* TableP;
typedef struct Object* ObjectP;
typedef int(*HashFcn)(const void *key, size_t tableSize);
typedef struct Object {
void *key;
ObjectLink *top;
} Object;
typedef struct Table{
ObjectLink *linkedObjects;
size_t size, originalSize;
HashFcn hfun;
PrintFcn pfun;
ComparisonFcn fcomp;
} Table;
And here I'm trying to make the call but get an error that I'm trying to access a place out of memory:
Boolean InsertObject(TableP table, ObjectP object)
{
int i = (*table->hfun)(object->key, table->size);
if (table->linkedObjects[i].key == NULL)
{
table->linkedObjects[i].key = object;
} else
{
table->linkedObjects[i].next->key = object;
}
return TRUE;
}
Using the Eclipse debugger I can tell that in the point of the call the values of the variables are:
object->key
type void*
value 0x804c018
table->size
type size_t
value 1
I guess this isn't the way to call a pointer to a function. What is wrong here?
EDIT:
in the debug i can also see:
*table->hfun
type int(const void *,size_t)
table->hfun
type HashFcn
value 0x11
Upvotes: 1
Views: 213
Reputation: 29985
You're not calling it the right way.
You can access a function pointer just like any other function.
table->hfun(object->key, table->size)
[Edit] Right, make sure you also assign the hfun
properly:
int myFunc(const void* key, size_t tableSize) { }
table->hfun = &myFunc;
Upvotes: 7