Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer-to-function with variable argument number

Reply
Thread Tools

Pointer-to-function with variable argument number

 
 
Andrej Prsa
Guest
Posts: n/a
 
      03-04-2004
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
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      03-04-2004
On Thu, 4 Mar 2004 01:15:11 +0100, Andrej Prsa <(E-Mail Removed)>
wrote:

>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
http://www.velocityreviews.com/forums/(E-Mail 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
 
Reply With Quote
 
 
 
 
Andrej Prsa
Guest
Posts: n/a
 
      03-04-2004
> 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
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      03-04-2004
In article <news:(E-Mail Removed) >
Leor Zolman <(E-Mail Removed)> writes:
>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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      03-04-2004
On 4 Mar 2004 05:33:23 GMT, Chris Torek <(E-Mail Removed)> wrote:

>In article <news:(E-Mail Removed) >
>Leor Zolman <(E-Mail Removed)> writes:
>>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.)


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 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
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      03-04-2004
Andrej Prsa <(E-Mail Removed)> wrote:
# 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;
}

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Haven't you ever heard the customer is always right?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
Variable argument number and behaviour [Newbie] Ryan Allan Ruby 6 07-02-2007 08:12 AM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
Variable argument number in functions Andrej Prsa C Programming 3 09-04-2003 05:15 PM



Advertisments