Possibility to define a function that could be used as lvalue

Discussion in 'C Programming' started by giuliano, Jan 10, 2007.

  1. giuliano

    giuliano Guest

    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?
     
    giuliano, Jan 10, 2007
    #1
    1. Advertising

  2. 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.
     
    Richard Heathfield, Jan 10, 2007
    #2
    1. Advertising

  3. 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: <mailto:>
     
    Kenneth Brody, Jan 10, 2007
    #3
  4. "giuliano" <> 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
     
    Malcolm McLean, Jan 11, 2007
    #4
  5. giuliano

    giuliano Guest

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

    "Malcolm McLean дµÀ£º
    "
    > "giuliano" <> 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
     
    giuliano, Jan 12, 2007
    #5
  6. giuliano

    giuliano Guest

    Hi all,

    Thanks a lot for all your reply. :p


    "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?
     
    giuliano, Jan 12, 2007
    #6
  7. giuliano

    santosh Guest

    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.
     
    santosh, Jan 12, 2007
    #7
  8. 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.
     
    Richard Heathfield, Jan 12, 2007
    #8
  9. "Richard Heathfield" <> 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.
     
    Malcolm McLean, Jan 13, 2007
    #9
  10. giuliano

    giuliano Guest

    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.
     
    giuliano, Jan 16, 2007
    #10
  11. giuliano

    giuliano Guest

    "Malcolm McLean дµÀ£º
    "
    > "Richard Heathfield" <> 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.


    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.
     
    giuliano, Jan 16, 2007
    #11
  12. "giuliano" <> writes:
    > 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?


    http://www.caliburn.nl/topposting.html
    http://www.cpax.org.uk/prg/writings/topposting.php

    It's also important to provide context when you post a followup
    (though in this case your meaning was clear enough). For more
    information on this:

    http://cfaj.freeshell.org/google/

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 16, 2007
    #12
  13. giuliano

    giuliano Guest

    "Keith Thompson дµÀ£º
    "
    > "giuliano" <> writes:
    > > 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?

    >
    > http://www.caliburn.nl/topposting.html
    > http://www.cpax.org.uk/prg/writings/topposting.php
    >
    > It's also important to provide context when you post a followup
    > (though in this case your meaning was clear enough). For more
    > information on this:
    >
    > http://cfaj.freeshell.org/google/


    Thanks for the information. It's a good lesson. :)

    Giuliano
     
    giuliano, Jan 16, 2007
    #13
  14. giuliano

    CBFalconer Guest

    giuliano wrote:
    >
    > 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)
     
    CBFalconer, Jan 16, 2007
    #14
    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. danny van elsen
    Replies:
    6
    Views:
    3,715
    Pete Becker
    May 7, 2005
  2. robin liu
    Replies:
    3
    Views:
    827
    Robin Liu
    Apr 21, 2006
  3. Kavya
    Replies:
    9
    Views:
    515
    Dik T. Winter
    Oct 28, 2006
  4. Replies:
    11
    Views:
    701
    James Kuyper
    Sep 22, 2008
  5. Julian Mehnle
    Replies:
    0
    Views:
    242
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page