func() in implementation and func (100, 200, 300) in calling

A

Alex Vinokur

------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}
 
R

Richard Heathfield

Alex Vinokur said:
------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}

It can't use the arguments, because it doesn't take any parameters.

Modify func so that it takes parameters. For example:

void func(int x, int y, int z)
{
/* you can now refer to x, y, and z within func */
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
A

Alex Vinokur

Richard Heathfield said:
Alex Vinokur said:


It can't use the arguments, because it doesn't take any parameters.

But that program is valid C-program.
Why does C allow that?
 
R

Richard Heathfield

Alex Vinokur said:
But that program is valid C-program.

Well, it's a C program that doesn't require the implementation to produce
any diagnostic messages. That isn't necessarily the same thing!
Why does C allow that?

Clearly func(100, 200, 300) is not a syntax error, since we can readily
imagine situations where we know it to be valid. Furthermore, the absence
of a prototype means that the constraint that the number of arguments must
match the number of parameters does not apply. Freely translated, this
means "it's your gun, and it's your foot - if it's really what you want to
do, fire away".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
R

Richard Heathfield

Tor Rustad said:
Richard Heathfield skrev:
Alex Vinokur said:
void func() {}
[...]
How can func() use its arguments 100, 200, 300?

It can't use the arguments, because it doesn't take any parameters.

Way too easy for R.H., now can he explain this:

(*(void(*)())0)();

Sure. It's line noise. Or, if you prefer, undefined behaviour. Std quotes
are from n1124:

6.3.2.3 Pointers:

"An integer constant expression with the value 0, or such an expression cast
to type void *, is called a null pointer constant. If a null pointer
constant is converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer to any object
or function."

Therefore, (void(*)())0 does not point to a function, and yet has function
pointer type. Thus, (*(void(*)())0)() is attempting to call a function that
does not exist.

6.5.3.2(4) says: "If an invalid value has been assigned to the pointer, the
behaviour of the unary * operator is undefined."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
T

Tor Rustad

Richard Heathfield skrev:
Tor Rustad said:
[...]
Way too easy for R.H., now can he explain this:

(*(void(*)())0)();

Sure. It's line noise. Or, if you prefer, undefined behaviour.
Std quotes are from n1124:

6.3.2.3 Pointers:

"An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.
If a null pointer constant is converted to a pointer type, the
resulting pointer, called a null pointer, is guaranteed to compare
unequal to a pointer to any object or function."

Well done! :)
Therefore, (void(*)())0 does not point to a function, and
yet has function pointer type.

So, as Mr. Heathfield correctly points out, the statement is a cast to
a function pointer, and then a function call:

typedef void (*fp) ();
( *(fp) 0) ();

Thus, (*(void(*)())0)() is attempting to call a function that does not exist.

"I once talked to someone who was writing a C program that was
going to run stand-alone in a small microprocessor. When this
machine was switched on, the hardware would call the subroutine
whose address was stored in location 0."
- Andrew Koenig, "C Traps and Pitfalls"
 

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,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top