MK Singh
MK Singh

Reputation: 706

How can we remove undeclared function error when function is declared with typedef in c?

When i have run the following piece of code:

typedef char *lrfield();

struct lrfields {
char name[26];
lrfield *f;
};

struct lrfields lr_table[] = {
    {"pri_tran_code1", pri_tran_code2},
    {"sec_tran_code", sec_tran_code},
    {"type_code", type_code},
    {"sys_seq_nbr", sys_seq_nbr},
    {"authorizer", authorizer},
    {"void_code", void_code},
    {"",0}
};

char *pri_tran_code2()
{
    return pri_tran_code;
}

*
*

if(second) 
{
     for(bp=lr_table; bp->name[0]; bp++)
     if(strcmp(bp->name, second)==0)
     {
         tmpval=bp->f();
         break;
     }
}

I have got these errors:

error: `pri_tran_code2' undeclared here (not in a function)
error: initializer element is not constant
error: (near initialization for `lr_table[0].f')
error: initializer element is not constant
error: (near initialization for `lr_table[0]')
error: initializer element is not constant
error: (near initialization for `lr_table[1]')

As you can see in the code that i have defined 'pri_tran_code2' above its call. Please help me to solve this error.

Upvotes: 1

Views: 1422

Answers (2)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143249

Add char *pri_tran_code2(); before you mention this name? Or simply move the whole implementation there. It doesn't matter where you call it, what matters is where you refer to it.

Upvotes: 3

user529758
user529758

Reputation:

Your declaration is erroneous. To declare a function (function-pointer) type, try this instead:

typedef char *(*lrfield)();

Upvotes: 3

Related Questions