Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22230

Re: Empty function parameter list

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Empty function parameter list
Date 2012-06-13 18:38 -0400
Organization A noiseless patient Spider
Message-ID <4FD9166B.4010500@verizon.net> (permalink)
References <e3aacc3b-f17b-4da8-a1e6-f54b3273ecf0@x21g2000vbc.googlegroups.com>

Show all headers | View raw


On 06/13/2012 05:48 PM, Maxim Fomin wrote:
> I have a question related to empty function parameter list: is it
> legal to call function with specific arguments via function pointer to
> same type with empty parameter list?
> 
> Test example is:
> 
> #include <stdio.h>
> 
> int (*funcptr)();
> 
> int foo(int x)
> {
> 	return x;
> }
> 
> int main()
> {
> 	funcptr = foo;
> 	printf("%d\n", funcptr(7));
> 	return 0;
> }
> 
> Compiler that I use issues warning for funcptr definition ("function
> declaration isn’t a prototype") but not for call.
> 
> n1570 says in 6.7.6.3-3 that an identifier list in a function
> declaration which is not a definition should be empty - as far I
> understand the statement, it surprisingly forbids features like void
> func(int x);

No, "int x" is an example of a parameter-type-list (6.7.6p1).
An identifier-list is something different that is part of the old K&R
style function definition syntax, which is still supported by the C
standard. It  has nothing to do with function prototypes. An old style
function definition looks like this:

	int func(x /* identifier list */)
	    int x;	/* declaration list */
	{
	    return x;
	}

What 6.7.6.3-3 is saying is that identifier lists can only occur in
function definitions, not in function declarations that are not definitions:

	int func(x);	/* CONSTRAINT VIOLATION */
	int func();	/* Permitted, but obsolescent */
	int func(int x);	/* Preferred. */

> In.14 it says that empty list for function definition means that the
> function has no arguments. I understand that for ex. int main(){...}
> and  int main(void){...} are the same.

It's actually been argued that they are not. Whether or not they're they
same matters only if there are recursive calls to main() farther down in
the same translation unit. But that gets into tricky issues that most
people don't have to worry about.

> But later it says that code void func(){...} is legal and that is
> provides no information about arguments. It also says that this is an
> "obsolete" way.

Yes. The identifier-list of an old-style function declaration is
optional; the parameter-type-list of a modern function prototype is not,
so if there's nothing between the '(' and the ')', it's an old-style
function declaration with no identifier-list.

> So, I found it unclear:
> 1) whether each function (non-definition) declaration parameter list
> should be empty

You should use function prototype declarations, with a non-empty
parameter-list; use 'void' if there are no arguments.
You should not use old-style function declarations. However, if you
choose to do so, their identifier lists must be empty.

> 2) whether void func(void) and void func() are equal

In function definitions, they both specify that the function takes no
parameters. However, in function declarations that are not function
definitions, they are significantly different:

void func1(void);
void func2();
void (*pfunc)(int x);
int x;
	func1(x);	// Constraint violation
	func2(x);	// Undefined behavior
	pfunc=func1;	// Constraint violation
	pfunc = func2;	// Permitted
	pfunc(x);	// Undefined behavior
	*(void(*)())pfunc)(x);	// Safe

> 3) related to what is described code obsolete

Old style function definitions and declarations.

> 4) (most important) are function calls via pointers to function with
> empty parameter list with some arguments valid (assuming as in the
> example that pointer points to proper function)

If the number of arguments doesn't match the number of parameters in the
function definition, the behavior is undefined. The difference is that
with function prototypes, attempting to do this is a constraint
violation, which is required to produce a diagnostic message, and is
likely to prevent your program from being compiled. With the old style
declarations, it's undefined behavior, and your compiler will probably
not be able to detect that fact if the function declaration and the
function definition are in different modules. You'll find out only at
run time, an only if the malfunctioning code malfunctions in a
sufficiently obvious way for you to notice.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Empty function parameter list Maxim Fomin <maxim@maxim-fomin.ru> - 2012-06-13 14:48 -0700
  Re: Empty function parameter list Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-13 18:36 -0400
    Re: Empty function parameter list James Kuyper <jameskuyper@verizon.net> - 2012-06-13 18:50 -0400
      Re: Empty function parameter list Maxim Fomin <maxim@maxim-fomin.ru> - 2012-06-15 02:08 -0700
        Re: Empty function parameter list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-15 11:30 +0100
  Re: Empty function parameter list James Kuyper <jameskuyper@verizon.net> - 2012-06-13 18:38 -0400
  Re: Empty function parameter list Dinesh Kalal <mr.dineshkalal@gmail.com> - 2012-06-15 01:35 -0700
    Re: Empty function parameter list James Kuyper <jameskuyper@verizon.net> - 2012-06-15 08:22 -0400

csiph-web