Possibility to define a function that could be used as lvalue

G

giuliano

Is it possible to define a function that could be used as
func() = 2;

I know in C++ using reference it could be realised.
Is it possible in C?
 
R

Richard Heathfield

giuliano said:
Is it possible to define a function that could be used as
func() = 2;

I know in C++ using reference it could be realised.
Is it possible in C?

C functions return rvalues, not objects. You can't assign a value to an
rvalue. You can, however, do this:

*func() = 2;

if you are prepared to live with the consequences and if func returns a
pointer to an appropriately-typed object.
 
K

Kenneth Brody

Richard said:
giuliano said:


C functions return rvalues, not objects. You can't assign a value to an
rvalue. You can, however, do this:

*func() = 2;

if you are prepared to live with the consequences and if func returns a
pointer to an appropriately-typed object.

Or a macro:

extern foo *real_func(void);
#define func() (*real_func())

I've seen errno defined in such ways, to allow thread-aware errors:

But your caveats still remain.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Malcolm McLean

giuliano said:
Is it possible to define a function that could be used as
func() = 2;

I know in C++ using reference it could be realised.
Is it possible in C?

Why is this post in some Chinese English character font?

You need a macro, not a function.

int global_x

#define lvalue() global_x
 
S

santosh

giuliano said:
Maybe it is because I posted it in China. :)

Please don't top-post. The quoted material belongs before, or
interspersed within, your text. Top-posting is considered rude on many
Usenet groups.
 
R

Richard Heathfield

santosh said:
Please don't top-post. The quoted material belongs before, or
interspersed within, your text. Top-posting is considered rude on many
Usenet groups.

I've seen that said so many times (not by you, I hasten to add), but I'd
like to place on record that, for my own part at least, I do *not* consider
top-posting to be rude. I *do* consider it to be thoughtless, foolish, and
indeed inconsiderate, but "rude" is not the word that springs to mind.
 
M

Malcolm McLean

Richard Heathfield said:
I've seen that said so many times (not by you, I hasten to add), but I'd
like to place on record that, for my own part at least, I do *not*
consider
top-posting to be rude. I *do* consider it to be thoughtless, foolish, and
indeed inconsiderate, but "rude" is not the word that springs to mind.
Most top-posters are simply unaware of the convention. A brief note should
produce the desired result.
 
G

giuliano

Please forgive me that I'm not even aware of what is happening here.
I don't quite catch you by top-posting.
Would you please give a little hint on it?
Thanks in advance.
 
G

giuliano

"Malcolm McLean дµÀ£º
"
Most top-posters are simply unaware of the convention. A brief note should
produce the desired result.

Oh, I see the convention now. I should put the quoted text befor my
post, right?
I'll watch out for that. Thanks for mentioning.
 
K

Keith Thompson

C

CBFalconer

giuliano said:
Please forgive me that I'm not even aware of what is happening here.
I don't quite catch you by top-posting.
Would you please give a little hint on it?
Thanks in advance.

The following links should fill you in.

--
Some informative links:
<http://members.fortunecity.com/nnqweb/> (newusers)
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top