Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Plz clarify

Reply
Thread Tools

Plz clarify

 
 
Naren
Guest
Posts: n/a
 
      07-23-2003
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.



 
Reply With Quote
 
 
 
 
Nick Austin
Guest
Posts: n/a
 
      07-23-2003
On Wed, 23 Jul 2003 16:43:11 +0530, "Naren"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-23-2003
Naren wrote:

> This works fine but I thought of replacing the two calls to hello
>
> By one call
>
> i = hello(&(i = hello(&i)));


That's still two calls, even if you fex the errors.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
Naren
Guest
Posts: n/a
 
      07-23-2003

"Nick Austin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 23 Jul 2003 16:43:11 +0530, "Naren"
> <(E-Mail Removed)> 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.



 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-23-2003

On Wed, 23 Jul 2003, Naren wrote:
>
> "Nick Austin" <(E-Mail Removed)> 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

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-23-2003
In <bflqkl$g54$(E-Mail Removed)> "Naren" <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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
plz clarify this doubt in vhdl chaitanyakurmala@gmail.com VHDL 1 09-22-2006 01:16 PM
model sim error plz clarify chaitanyakurmala@gmail.com VHDL 1 09-22-2006 10:34 AM
Size of my Struct? PLZ PLZ reply brian C Programming 7 11-25-2004 01:30 AM
Re: plz help!!! plz plz plz plzplzplz help the noob alkzy Microsoft Certification 0 10-31-2004 10:04 PM
Computer Problems... Plz Plz Plz HELP ME..... Nick Computer Support 0 06-04-2004 08:50 PM



Advertisments