Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Possibility to define a function that could be used as lvalue

Reply
Thread Tools

Possibility to define a function that could be used as lvalue

 
 
giuliano
Guest
Posts: n/a
 
      01-10-2007
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?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-10-2007
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      01-10-2007
Richard Heathfield wrote:
>
> 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.


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: <(E-Mail Removed)>


 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-11-2007

"giuliano" <(E-Mail Removed)> wrote in
> 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


 
Reply With Quote
 
giuliano
Guest
Posts: n/a
 
      01-12-2007
Maybe it is because I posted it in China.

"Malcolm McLean д
"
> "giuliano" <(E-Mail Removed)> wrote in
> > 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


 
Reply With Quote
 
giuliano
Guest
Posts: n/a
 
      01-12-2007
Hi all,

Thanks a lot for all your reply.


"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?


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-12-2007
giuliano wrote:
> 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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-12-2007
santosh said:

> giuliano wrote:
>> 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.


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-13-2007

"Richard Heathfield" <(E-Mail Removed)> wrote in message
>
>> giuliano wrote:
>>> 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.

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


 
Reply With Quote
 
giuliano
Guest
Posts: n/a
 
      01-16-2007
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.

 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM



Advertisments