Plz clarify

N

Naren

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.
 
N

Nick Austin

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.
 
C

Chris Dollin

Naren said:
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.
 
A

Arthur J. O'Dwyer

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
 
D

Dan Pop

In said:
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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top