Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > func() in implementation and func (100, 200, 300) in calling

Reply
Thread Tools

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

 
 
Alex Vinokur
Guest
Posts: n/a
 
      11-17-2006
------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}
-------------------

How can func() use its arguments 100, 200, 300?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-17-2006
Alex Vinokur said:

> ------ foo.c ------
> void func() {}
> int main()
> {
> func (100, 200, 300);
> return 0;
> }
> -------------------
>
> How can func() use its arguments 100, 200, 300?


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.
 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      11-17-2006
"Richard Heathfield" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Alex Vinokur said:
>
> > ------ foo.c ------
> > void func() {}
> > int main()
> > {
> > func (100, 200, 300);
> > return 0;
> > }
> > -------------------
> >
> > How can func() use its arguments 100, 200, 300?

>
> 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?

>
> 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 */
> }
>


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-17-2006
Alex Vinokur said:

> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Alex Vinokur said:
>>
>> > ------ foo.c ------
>> > void func() {}
>> > int main()
>> > {
>> > func (100, 200, 300);
>> > return 0;
>> > }
>> > -------------------
>> >
>> > How can func() use its arguments 100, 200, 300?

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

>
> 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.
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      11-17-2006
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)();

?

--
Tor <torust AT online DOT no>

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-18-2006
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.
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      11-18-2006
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"

--
Tor <torust AT online DOT no>

 
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
typedef func *func(); Till Backhaus C Programming 4 12-14-2006 04:31 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
difference between import from mod.func() and x=mod.func() Hari Sekhon Python 0 06-20-2006 08:07 AM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM
int func() v/s int func(void) Vinko Vrsalovic C Programming 14 01-24-2005 08:07 AM



Advertisments