Pointer-to-function with variable argument number

A

Andrej Prsa

Hi!

Why do I get a warning about incompatible pointer type if I try to assign
a pointer to the function with variable argument number:

int func (int argc, ...) ,

but everything is ok when I'm using a simple function like:

int func (int argc, char *whatever)

Can't I point to the function with variable number of arguments?

Best wishes,

Andrej
 
L

Leor Zolman

Hi!

Why do I get a warning about incompatible pointer type if I try to assign
a pointer to the function with variable argument number:

int func (int argc, ...) ,

but everything is ok when I'm using a simple function like:

int func (int argc, char *whatever)

Can't I point to the function with variable number of arguments?

Best wishes,

Andrej

Things just have to match. To point to a function with a variable number of
args, use a pointer to a function that takes a variable number of args:

int foo(...);
int bar(int i);

int main()
{
int (*pf)(...);
int (*pb)(int);

pf = foo; // OK
pf = bar; // error
pb = bar; // OK
pb = foo; // error
return 0;
}


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
A

Andrej Prsa

Things just have to match. To point to a function with a variable number
of args, use a pointer to a function that takes a variable number of
args:

int foo(...);
int bar(int i);

int main()
{
int (*pf)(...);
int (*pb)(int);

pf = foo; // OK
pf = bar; // error
pb = bar; // OK
pb = foo; // error
return 0;
}

Great, this makes a lot of sense! Thanks,

Andrej
 
C

Chris Torek

Things just have to match. To point to a function with a variable number of
args, use a pointer to a function that takes a variable number of args:

Right, but:
int foo(...);

this type does not exist in C. Variable-arguments functions must
have at least one initial fixed argument. You might make this:

int foo(int, ...);

if it takes some number of "int" parameters ending with a -1, even
if the "some number" is allowed to be zero (so that the first int
is just -1).

(I think this is a broken part of the C Standards, but it is still
there and we are stuck with it. It is not a serious flaw, just a
minor one, like the lack of zero-sized objects.)
 
L

Leor Zolman

Right, but:


this type does not exist in C. Variable-arguments functions must
have at least one initial fixed argument. You might make this:

int foo(int, ...);

if it takes some number of "int" parameters ending with a -1, even
if the "some number" is allowed to be zero (so that the first int
is just -1).

(I think this is a broken part of the C Standards, but it is still
there and we are stuck with it. It is not a serious flaw, just a
minor one, like the lack of zero-sized objects.)

Thanks! I wasn't aware of that, and when testing I must have forgotten to
give the Comeau compiler the --c99 option. Even if you compile a file with
a .c extension, that compiler (which happens to be my favorite) defaults to
C++ mode. Arghhh. I may have to just modify my 4NT compile script to add
that option automatically when it detects I'm compiling with Comeau...
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
D

Derk Gwen

# Hi!
#
# Why do I get a warning about incompatible pointer type if I try to assign
# a pointer to the function with variable argument number:
#
# int func (int argc, ...) ,
#
# but everything is ok when I'm using a simple function like:
#
# int func (int argc, char *whatever)
#
# Can't I point to the function with variable number of arguments?

If you want to assign functions with various arguments, you can use a
cast or old-style functions. Doing so is, of course, less safe.

typedef void (*func)(void);
typedef void (*old)();

void a(b,c) int b,c; {;}
void w(x,y,z) double x,y,z; {;}
int p(int q,void *r) {;}

void t(void) {
func P; old Q;
P = (func)a; /*casts*/
P = (func)w;
P = (func)p;
Q = a; /*old style functions*/
Q = w;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top