Plz clarify

Discussion in 'C Programming' started by Naren, Jul 23, 2003.

  1. Naren

    Naren Guest

    Hello grp,
    I have a doubt.
    This is an example program I have written.
    May not be useful but just wanted to understand.
    short int hello(short int *i);
    main()
    {

    short int i=2;
    i = hello(&i);
    i = hello(&i);
    printf("%d",i);
    }

    short int hello(short int *i)
    {
    short int ret = *i;
    return ret;
    }

    This works fine but I thought of replacing the two calls to hello

    By one call

    i = hello(&(i = hello(&i)));

    I get two errors expected l-value and
    too few parameters..
    Could anyone kindly explain me this.

    Thaanx in advance.
    Rgds,
    Naren.
     
    Naren, Jul 23, 2003
    #1
    1. Advertising

  2. Naren

    Nick Austin Guest

    On Wed, 23 Jul 2003 16:43:11 +0530, "Naren"
    <> wrote:

    >short int hello(short int *i);
    >main()
    >{
    >
    > short int i=2;
    > i = hello(&i);
    > i = hello(&i);
    > printf("%d",i);
    >}
    >
    >short int hello(short int *i)
    >{
    > short int ret = *i;
    > return ret;
    >}
    >
    >This works fine but I thought of replacing the two calls to hello
    >
    >By one call
    >
    >i = hello(&(i = hello(&i)));


    i = hello( (i = hello(&i), &i) );

    Nick.
     
    Nick Austin, Jul 23, 2003
    #2
    1. Advertising

  3. Naren

    Chris Dollin Guest

    Chris Dollin, Jul 23, 2003
    #3
  4. Naren

    Naren Guest

    "Nick Austin" <> wrote in message
    news:...
    > On Wed, 23 Jul 2003 16:43:11 +0530, "Naren"
    > <> wrote:
    >
    > >short int hello(short int *i);
    > >main()
    > >{
    > >
    > > short int i=2;
    > > i = hello(&i);
    > > i = hello(&i);
    > > printf("%d",i);
    > >}
    > >
    > >short int hello(short int *i)
    > >{
    > > short int ret = *i;
    > > return ret;
    > >}
    > >
    > >This works fine but I thought of replacing the two calls to hello
    > >
    > >By one call
    > >
    > >i = hello(&(i = hello(&i)));

    >
    > i = hello( (i = hello(&i), &i) );


    This is too good.but could you explain to me why mine went wrong?
    >
    > Nick.
     
    Naren, Jul 23, 2003
    #4
  5. On Wed, 23 Jul 2003, Naren wrote:
    >
    > "Nick Austin" <> wrote...
    > > On Wed, 23 Jul 2003 16:43:11 +0530, "Naren" wrote...
    > > >
    > > >main()
    > > >{
    > > >
    > > > short int i=2;
    > > > i = hello(&i);
    > > > i = hello(&i);
    > > > printf("%d",i);
    > > >}
    > > >
    > > >This works fine but I thought of replacing the two calls to hello
    > > >
    > > >By one call
    > > >
    > > >i = hello(&(i = hello(&i)));

    > >
    > > i = hello( (i = hello(&i), &i) );

    >
    > This is too good.but could you explain to me why mine went wrong?


    Yours tries to take the address of (i = hello(&i)), which is just
    silly. (i = ...) is an expression, not an lvalue - it's like trying
    to take the address of (i - 1), or (42). You just can't do it in
    the C programming language, because the expression's value doesn't
    necessarily have to "exist" anywhere that has an address to be
    taken!

    Nick's fix uses the "comma operator", which is *completely different*
    from the commas used to separate function arguments. The comma operator
    introduces a "sequence point" between the expression (i = hello(&i))
    and the expression (&i), and returns the value of the second expression
    (&i). That's the value that gets passed to 'hello' the second time.

    Sequence points should be covered in the FAQ; search Google for
    "comp.lang.c FAQ".

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Jul 23, 2003
    #5
  6. Naren

    Dan Pop Guest

    In <bflqkl$g54$> "Naren" <> writes:

    >This is an example program I have written.
    >May not be useful but just wanted to understand.
    >short int hello(short int *i);
    >main()


    You may want to get into the habit of writing

    int main()

    which is required by C99 and even by certain C89 compilers operating in
    "picky" mode.

    >{
    >
    > short int i=2;
    > i = hello(&i);
    > i = hello(&i);
    > printf("%d",i);


    You can't call printf without a proper declaration in scope. And it's
    not wise to omit the newline character at the of the last line output
    by your program.

    >}
    >
    >short int hello(short int *i)
    >{
    > short int ret = *i;
    > return ret;
    >}
    >
    >This works fine but I thought of replacing the two calls to hello
    >
    >By one call
    >
    >i = hello(&(i = hello(&i)));


    To replace the two calls to hello by one call, simply drop one of your

    i = hello(&i);

    lines.

    >I get two errors expected l-value and
    >too few parameters..
    >Could anyone kindly explain me this.


    i = hello(&i) is an expression that doesn't yield an lvalue, therefore
    you can't take its address. There are simpler ways of calling hello()
    twice in the same expression (although this is NOT what you said you want)

    i = hello(&i), hello(&i);
    or
    i = (hello(&i), hello(&i));

    Can you tell the difference between the two?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 23, 2003
    #6
    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. Replies:
    1
    Views:
    467
    Jonathan Bromley
    Sep 22, 2006
  2. Replies:
    1
    Views:
    498
  3. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    423
    -berlin.de
    Nov 25, 2004
  4. Replies:
    1
    Views:
    391
  5. Replies:
    2
    Views:
    344
    Nick Keighley
    Nov 24, 2006
Loading...

Share This Page