Dean
Dean

Reputation: 8998

Write typedef function

In the header file I have the following line:

typedef int comparator(int* left, int* right);

But when I tried to write a function definition for it like so:

int comparator (int* left, int* right){
    if(left<right) {
        return 1;
    } else if(right>left) {
        return -1;
    } else {
        return 0;
    }   
}

The error I get is:

file.c:10: error: ‘comparator’ redeclared as different kind of symbol

The function must be typedef'ed because later on in the header file it is used in a method prototype like so:

struct bst_node** search(struct bst_node** root, comparator compare, void* data);

So how is this method constructed?

Upvotes: 0

Views: 691

Answers (3)

effeffe
effeffe

Reputation: 2881

If the typedef is not a mistake, you now have a type called comparator and you cannot use this identifier for a function, no matter if the existing type is a function pointer, it would be the same as writing this:

typedef int myType;

int myType(int, int);

You can use that typedef in something like this:

typedef int (* comparator)(int, int);

int comparison(int, int);
void sort(int [], comparator);

int main(void)
{
  int array[10];
  sort(array, comparison);
}

If you're looking for a function declaration and that typedef was a mistake, then other answers already provide you the solution.

EDIT: Maybe I'm wrong, but reading comments, it seems like you're expecting too much from typedef. Functions have their types (main could have type int ()(void) or int ()(int, char **)), and you're just saying that comparator can be used as a type and it means int ()(int *, int *).

If you write:

typedef double real;

You're not creating (technically, defining) any double object, you're just saying "from now on, real means double".

Upvotes: 2

Keith Nicholas
Keith Nicholas

Reputation: 44298

in your header file you should just have

 int comparator(int* left, int* right);

your aren't declaring a type, but just declaring a function.

you could declare a function pointer type called comparator...

typedef int (*comparator)(int* left, int* right);

but you wouldn't then make a function called comparator. you just need to make a function with the same signature

int ascending_comparator(int* left, int* right)
{
    // comparison
} 

then call

search(root, ascending_comparator, data);

Also, I got a feeling you don't want to be passing int* but actual int, or in your function you don't want to be comparing the pointers, but comparing the values of what each int is pointing to, like

int comparator (int* left, int* right){
    if(*left < *right) {
        return 1;
    } else if(*right > *left) {
        return -1;
    } else {
        return 0;
    }   
}

but possibly you are wanting to compare the memory locations where the ints are stored.

also your comparisons are the same

if(left < right) 

is the same as

if( right > left)

so maybe you want

 int comparator (int* left, int* right){
        if(*left < *right) {
            return 1;
        } else if(*left > *right) {
            return -1;
        } else {
            return 0;
        }   
    }

Upvotes: 4

user529758
user529758

Reputation:

I suspect you don't actually want to create a typedef, rather you want to declare the function using a prototype. It's sufficient to write

int comparator(int *left, int *right);

Upvotes: 3

Related Questions