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

Discussion in 'C Programming' started by Alex Vinokur, Nov 17, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Nov 17, 2006
    #1
    1. Advertising

  2. 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.
    Richard Heathfield, Nov 17, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Richard Heathfield" <> wrote in message news:...
    > 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
    Alex Vinokur, Nov 17, 2006
    #3
  4. Alex Vinokur said:

    > "Richard Heathfield" <> wrote in message
    > news:...
    >> 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.
    Richard Heathfield, Nov 17, 2006
    #4
  5. Alex Vinokur

    Tor Rustad Guest

    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>
    Tor Rustad, Nov 17, 2006
    #5
  6. 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.
    Richard Heathfield, Nov 18, 2006
    #6
  7. Alex Vinokur

    Tor Rustad Guest

    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>
    Tor Rustad, Nov 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    3
    Views:
    494
    Jonathan Turkanis
    Jan 24, 2004
  2. Xiangliang Meng

    What does "Uint32 enum {a = 100, b = 200};" mean?

    Xiangliang Meng, Jul 13, 2004, in forum: C Programming
    Replies:
    9
    Views:
    577
    Keith Thompson
    Jul 20, 2004
  3. sanket
    Replies:
    7
    Views:
    980
    Tsung
    Nov 3, 2011
  4. fred
    Replies:
    3
    Views:
    256
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    861
Loading...

Share This Page