znlyj
znlyj

Reputation: 1149

What do these function typedefs mean?

I am trying to understand what the following typedefs mean. Are they function pointers?

typedef int Myfunc(char *);

static Myfunc myfunc;

int myfunc(char *string)
{
    printf("%s\n", string);
    return 0;
}

I know typedef int Myfunc(char *) means func Myfunc return an integer,that's all,all right? And I thought, next statement, how could call myfunc? It should be this way static Myfunc *myfunc, mean a function pointer,isn't it?

Upvotes: 2

Views: 420

Answers (4)

hmjd
hmjd

Reputation: 121971

To call myfunc is the same as a function call:

myfunc("a-string");

Upvotes: 1

Brett Hale
Brett Hale

Reputation: 22318

The signature for the myfunc is: typedef int (*MyFunc)(char *); Then you can declare a variable of type MyFunc i.e.,

static MyFunc func_ptr;

You can then assign a function matching the signature to this variable.

Upvotes: 1

Matt Joiner
Matt Joiner

Reputation: 118500

I'm not sure that's valid code.

typedef int (*Myfunc)(char *);

declares a type Myfunc, that is a pointer to a function that takes a char * and returns int.

You cannot forward declare a function with a typedef. Omit static Myfunc myfunc; and instead begin your function definition with

static int myfunc(char *string) {

Upvotes: 0

user23743
user23743

Reputation:

The second line is a declaration of a function, not a function pointer. The function is of type MyFunc, is called myfunc, and has static linkage: meaning that the function is not available to other source files compiled into the same object.

Upvotes: 3

Related Questions