Can anybody tell me the reason

Discussion in 'C++' started by Ashu, Jul 4, 2006.

  1. Ashu

    Ashu Guest

    can anybody tell me the reason why following code is not working?


    int *funct()
    {
    int p=10;
    return &p;
    }


    int *ptr;
    funct() = ptr; //------->Giving Compile time error
    (LValue)




    can anybody plz tell me the reason of this.
     
    Ashu, Jul 4, 2006
    #1
    1. Advertisements

  2. First do not return address of local variable. This is not a compile
    error but the effect is undefined and it may lead to

    exception. Compiler gives a warning on this statement.
    it should be like this..

    ptr = funct();

    we should assign a value to left hand side variable (even in general
    math).

    -- Murali Krishna.
     
    Murali Krishna, Jul 4, 2006
    #2
    1. Advertisements

  3. Ashu

    Jim Langston Guest

    func() is a rhv (Right hand value).
    ptr is a lhv (Left hand value/variable).
    In C and C++ you put on the left the thing you want to get changed (the
    left hand value).

    ptr = func();
    means, Load into ptr the value of func().

    func() = ptr;
    does not work because you can not load a function with a value, you can only
    pass them values (well, in some cases you can in c++, but that's a different
    story).
     
    Jim Langston, Jul 4, 2006
    #3
  4. Ashu

    Geo Guest

    Looks like you haven't bothered to read even the most basic C++ book
    !!!!
     
    Geo, Jul 4, 2006
    #4
  5. Ashu

    Ashu Guest








    Boss I think u need to study some book nd for kind information of u
    all, u can assign like this

    func() = ptr


    there is no problem in that if ptr is reference to some int and func()
    also returning some reference but the problem is coming in case of
    pointers only and i want to know why.

    and please please u all who are saying that this is wrong or we do it
    other way in maths also , study some good books about LValue and then u
    will know that this works and how exactly C++ works.
    nyways thanks for ur replies
     
    Ashu, Jul 5, 2006
    #5
  6. You're trying to return the address of a local. That won't
    work, because the local ceases to exist when you exit funct.
    You failed to initializ ptr, so you don't know where it's
    pointing, nor do you know what it's pointing at.
    There's 3 different errors there:
    1. Uninitialized pointer
    2. Unknown target
    3. Assigning to an rvalue

    If you're trying to get one function to alter a variable in
    another, that can be done, but you'd have to declare p as "static"
    and return a reference, like so:

    #include <iostream>
    int& funct()
    {
    static int p = 10;
    std::cout << p << std::endl;
    return p;
    }

    int main()
    {
    int x = 17;
    funct() = x; // prints "10", then assigns 17 to "p" in "funct"
    funct(); // prints "17"
    return 0;
    }


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    lonewolfintj at pacbell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
     
    Robbie Hatley, Jul 5, 2006
    #6
  7. Ashu

    Ian Collins Guest

    Can we have that again in something approaching English?
     
    Ian Collins, Jul 5, 2006
    #7
  8. Ashu

    Geo Guest

    Besides the fact that you code is completely broken, the fact that it
    doesn't even compile suggests that it isn't in fact "how exactly C++
    works".
     
    Geo, Jul 5, 2006
    #8
  9. Hi Ashu,

    I think you are from India (me too).
    My friend, don't just start an argument. People here know more than
    you and me.
    I have seen people from all over the world answering to complex
    questions.
    Most of the time I try to analyze what they write.

    If you or me really know how C++ works, we wouldn't have come here.
    And this is your second post..
    Don't be such emotional. C++ is just one more language to learn.

    and in math, I never did in that way.

    -- Murali Krishna
     
    Murali Krishna, Jul 5, 2006
    #9
  10. Ashu

    Ashu Guest


    Hi Murali

    Thanks buddy for ur reply and I am sorry to all for my remarks.

    Actually you are right we all are like small fish in this sea of C++
    which is too deep to master fully........

    nyways thanks again for the replies, hope to put more such studid
    queries soon buddies...

    bye
     
    Ashu, Jul 5, 2006
    #10
  11. Hi Murali
    a happy and sentimental ending in Indian cinema way..
    not a sea. Faculties tell them in that way.. that Java is an ocean,
    C++ is vast.

    I can't learn some thing if it is like an ocean.

    what does buddies mean? I really dont know.

    -- Murali Krishna
     
    Murali Krishna, Jul 5, 2006
    #11
  12. If func() returns an Lvalue, you can do that.

    However, a pointer by itself is an rvalue, just like an int:

    int getInt() { return ... }
    int* getPointer() { return ... }

    int i;

    getInt() = 10; // won't work
    getPointer() = &i; // won't work

    But a pointer can be used to form an lvalue. By dereferencing the
    pointer, you get an lvalue for the pointee:

    *getPointer() = 10; // works (or better: compiles)

    But!! the function above is dangerous:

    This will return a pointer to a local variable, which lifetime ends by
    leaving the function. If you dereference the pointer, you invoke
    undefined behaviour.

    You can return a pointer to a static variable, which is often used for
    singletons (but I would return a reference):

    class Singleton;

    Singleton* getSingleton()
    {
    static Singleton instance;
    return &instance;
    }
     
    Thomas J. Gritzan, Jul 5, 2006
    #12
  13. "Buddy" is a variation on "chum", "mate", "pal", etc. They all mean
    something like "friend".

    HTH :)
    Stu
     
    Stuart Golodetz, Jul 5, 2006
    #13
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.