473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Pointer-to-function with variable argument number

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
Nov 14 '05 #1
5 1672
On Thu, 4 Mar 2004 01:15:11 +0100, Andrej Prsa <an*********@guest.arnes.si>
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
le**@bdsoft.com
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
Nov 14 '05 #2
> 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
Nov 14 '05 #3
In article <news:ai********************************@4ax.com >
Leor Zolman <le**@bdsoft.com> 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 (40°39.22'N, 111°50.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.
Nov 14 '05 #4
On 4 Mar 2004 05:33:23 GMT, Chris Torek <no****@torek.net> wrote:
In article <news:ai********************************@4ax.com >
Leor Zolman <le**@bdsoft.com> 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
le**@bdsoft.com
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
Nov 14 '05 #5
Andrej Prsa <an*********@guest.arnes.si> 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?
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
27
by: Riaan Cillié | last post by:
Hi I'm trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
4
by: code break | last post by:
Hi all, What is the difference between stack pointer and frame pointer ? Any suggestions are welcome ,,,
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
18
by: c.a.l | last post by:
a) If i do pointer = pointer_to_safe_thing - 1000; pointer == pointer_to_safe_thing; then I am *not* accessing invalid memory. Nor i am incorrect in mathematical sense. Nevertheless outcome...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.