Indrajeet
Indrajeet

Reputation: 45

Unable to understand C function call

I have a function with definition :

int foobar(char *ptr,...)

the function call is as follows :

int (*fooptr) (char *,...) = foobar;

I am not able to understand how is the function getting called ... Thanks in advance

Upvotes: 1

Views: 148

Answers (5)

MByD
MByD

Reputation: 137272

This is a varargs function, which can receive a variable number of parameters (similar to printf). the second line you give is an assignment, not a function call.

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

The function is not getting called in your example. Its address is stored in the fooptr variable, which is a function pointer. If you later call that function pointer while it's still pointing to foobar function, it'll call foobar function.

You can write the second line as:

// declare fooptr as a variable of type function pointer 
// taking (char*,...) and returning int
int (*fooptr) (char *,...);  
// take the address of foobar function and assign it to fooptr
fooptr = &foobar;

to make it clearer.

Upvotes: 0

jtbandes
jtbandes

Reputation: 118651

The function is not getting called with the code you have posted. The first line is the function declaration, the second is creating a pointer to it. To call it you have to use foobar(myCharPtr[, other arguments]) or fooptr(myCharPtr[, other arguments]).

Upvotes: 0

Ray Toal
Ray Toal

Reputation: 88378

It's not a call. It is a declaration of fooptr.

Upvotes: 0

sje397
sje397

Reputation: 41802

That's not a function call.

It is declaring a function pointer variable called fooptr that holds the address of the function.

To call that function via the pointer you would do e.g.:

int return_value = (*fooptr)(char_ptr, x, y, z);

Upvotes: 8

Related Questions