473,406 Members | 2,217 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,406 software developers and data experts.

void argument

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng

Oct 8 '05 #1
13 6327
Pe*******@gmail.com wrote:
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.


Nope, they're exactly the same thing in C++: fun takes no arguments. For
a function taking any number of arguments use ellipses, e.g. void fun(...);

Jacques.
Oct 8 '05 #2
<Pe*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng


No, in C++ they mean the same: a function which takes no arguments. The
business about taking any number of arguments is disallowed in C++. Because
of overloading, the argument types are part of the function's signature.

--
Cy
http://home.rochester.rr.com/cyhome/
Oct 8 '05 #3
No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file

Oct 8 '05 #4
In article <11********************@f14g2000cwb.googlegroups.c om>, Nan Li
<na******@gmail.com> writes
No.


No what?

If you are replying top something quote the message you are replying to.

If you are using the broken google interface to usenet please set the
options properly.


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Oct 8 '05 #5
ben
Pe*******@gmail.com wrote:
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng

No. In C++, the above two prototypes are exactly the same.

Ben
Oct 8 '05 #6
On 2005-10-08 18:48:19 -0400, Jacques Labuschagne
<ja*****@clawshrimp.com> said:
Pe*******@gmail.com wrote:
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.


Nope, they're exactly the same thing in C++: fun takes no arguments.
For a function taking any number of arguments use ellipses, e.g. void
fun(...);


Actually, no. You cannot define a function taking any number of
arguments that way. Using the ellipsis (even in C), requires at least
one non variadic parameter, i.e.:

/* This function requires at least one int argument, but can take any
number of additional, non-POD arguments */
void fun(int, ...)
{
}

/* This function definition is illegal:*/
void fun2(...)
{
}

--
Clark S. Cox, III
cl*******@gmail.com

Oct 9 '05 #7
* Clark S. Cox III:
On 2005-10-08 18:48:19 -0400, Jacques Labuschagne
<ja*****@clawshrimp.com> said:
Pe*******@gmail.com wrote:
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.


Nope, they're exactly the same thing in C++: fun takes no arguments.
For a function taking any number of arguments use ellipses, e.g. void
fun(...);


Actually, no. You cannot define a function taking any number of
arguments that way. Using the ellipsis (even in C), requires at least
one non variadic parameter, i.e.:


Not in C++ (I don't know about C).

There are two forms of parameter-declaration-clause,

parameter-declaration-list[OPT] ...[OPT]
parameter-declaration-list, ...

It seems you've focused only the second form.

You might ask, what would something like void foo(...){} be good for, since
there's no argument there to anchor the argument retrieval?

And one answer is that the ellipsis ... ranks lowest of all possible matches
for an actual argument, and for this usage it would just be silly to require
an extra argument in front.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 9 '05 #8

<Pe*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.


No, and that is a very good thing.
When I was C beginner, that lack of prototyping in C was a real problem
for me (and I dind't know that one should always prototype functions
back in 1987.).

int main()
{
int i = malloc(32);
int *p = malloc(32);
free(i);
free(p);
return 0;
}

This C code compiles and links without problem, but causes lot of trouble.

Greetings, Bane.
Oct 9 '05 #9
Alf P. Steinbach wrote:
You might ask, what would something like void foo(...){} be good for,
since there's no argument there to anchor the argument retrieval?


My question would be how to retrieve the arguments, since va_start needs the
last fixed argument, which in this case doesn't exist.

Oct 9 '05 #10
On Sun, 09 Oct 2005 00:53:30 GMT, al***@start.no (Alf P. Steinbach)
wrote:
You might ask, what would something like void foo(...){} be good for, since
there's no argument there to anchor the argument retrieval?


I've only seen this syntax used in catch statements (i.e. as a
"catch-all" last resort). It is sometimes dangerous, though --
depending on the platform, it could be a source of memory leaks if
used indiscriminately.

--
Bob Hairgrove
No**********@Home.com
Oct 9 '05 #11
* Rolf Magnus:
Alf P. Steinbach wrote:
You might ask, what would something like void foo(...){} be good for,
since there's no argument there to anchor the argument retrieval?


My question would be how to retrieve the arguments, since va_start needs the
last fixed argument, which in this case doesn't exist.


That's the same question; already answered. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 9 '05 #12
On Sat, 08 Oct 2005 15:58:27 -0700, Nan Li wrote:
No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file


There's a difference between a function prototype ("declaration") and a
function definition.

Change the above "foo" to:

extern void foo();

and it will work fine. The assumption is that there is a real function
somewhere that has some sort of arguments. The danger is you're not
matching the real argument definition, so the compiler has to guess based
on the arguments you pass.

Of course, that's all C, so I may have a detail or two wrong...

- Jay
Oct 9 '05 #13

"Jay Nabonne" <ja*********@sonicNOSPAM.com> wrote in message
news:pa***************************@sonicNOSPAM.com ...
On Sat, 08 Oct 2005 15:58:27 -0700, Nan Li wrote:
No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file


There's a difference between a function prototype ("declaration") and a
function definition.

Change the above "foo" to:

extern void foo();

and it will work fine. The assumption is that there is a real function
somewhere that has some sort of arguments. The danger is you're not
matching the real argument definition, so the compiler has to guess based
on the arguments you pass.

Of course, that's all C, so I may have a detail or two wrong...


function definition with empty parenthesis in c also means no arguments.
this is valid only for declaration, so you are right, but g++ would compile
it
as c++ anyway; one must use gcc for c compiling.

void foo();

int main()
{
foo(1);
foo(1,'c',3.2);
return 0;
}

void foo()
{
}

compiles with no problem in c.

Greetings, Bane.

Oct 9 '05 #14

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

Similar topics

52
by: Vladimir Grul | last post by:
Hello, I have a class member function declared as class some_class { .... virtual int call(void); }; Can I use this-> inside the function body?
5
by: Manuel Maria Diaz Gomez | last post by:
Thanks Victor for your previous answer! I have the following problem: I would like to implement an interface class that defines a pure virtual method that could take any parameter as input, ...
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
17
by: codeslinger | last post by:
What is the point of the construct in the subject line? Why the use of two const labels? Does that do something different than const void *x? And what would void * const x indicate? Thanks in...
6
by: MackS | last post by:
Hi FAQ 4.9 states that void** cannot be used portably "to pass a generic pointer to a function by reference". I would like to hear from you whether there is anything wrong with the following...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
4
by: LordHog | last post by:
Hello all, I am having a little problem getting my (void *) assignment to work correctly. Basically it is some code for a circular queue buffer I am trying to implement, but getting the...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
4
by: Andreas Klimas | last post by:
hello, no many words, I will start with an example instead int foo(void *r) {...} void test(void) { int *x=0; foo(x); /* seems to me as valid */ }
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.