return pointer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: return pointer

  1. #1
    Join Date
    Apr 2013
    Posts
    42

    Unhappy return pointer

    The code below is supposed to fill, show, and revalue property. The fill function is supposed to return a pointer that creates a range of property values. The show function is supposed to show the property values entered and the revalued property values. I think part of the problem is the returned pointer from the fill function. Once that is cleared up, I think I will find more problems.

    Code:
    #include <iostream>
    const int Max = 5;
    
    // function prototypes
    double fill_array(double ar[], int limit);
    void show_array(double * begin, double * end);
    void revalue(double r, double ar[], const double * begin, const double * end);
    
    int main()
    {
        using namespace std;
        double properties[Max];
    
        double pk = fill_array(properties, Max);
        show_array(properties, &pk);
        if (pk > 0)
        {
            cout << "Enter revaluation factor: ";
            double factor;
            while (!(cin >> factor))    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; Please enter a number: ";
            }
            revalue(factor, properties, properties, &pk);
            show_array(properties, &pk);
        }
        cout << "Done.\n";
        // cin.get();
        // cin.get();
        return 0;
    }
    
    double fill_array(double ar[], int limit)
    {
        using namespace std;
        double temp;
        double * pt;
        int i;
        for (i = 0; i < limit; i++)
        {
            cout << "Enter value #" << (i + 1) << ": ";
            cin >> temp;
            if (!cin)    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; input process terminated.\n";
               break;
            }
            else if (temp < 0)     // signal to terminate
                break;
            ar[i] = temp;
        }
        pt = ar + i;
        return *pt;
    }
    
    // the following function can use, but not alter,
    // the array whose address is ar
    void show_array(double * begin, double * end)
    {
        using namespace std;
        const double * pt;
        for (pt = begin; pt != end; pt++)
        {
            cout << "Property #" << (pt + 1) << ": $";
            cout << pt << endl;
        }
    }
    
    // multiplies each element of ar[] by r
    void revalue(double r, double ar[], const double * begin, const double * end)
    {
        const double * pt;
        double revalue = 0;
        for (pt = begin; pt != end; pt++)
        {
            revalue = revalue * *pt;
        }
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,132

    Re: return pointer

    What's the value of i at the end of your fill function?

    Also, it's not returning a pointer, it's returning a double.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,572

    Re: return pointer

    As GCDEF said, what is the value of i at the end of your fill function? So what value are you returning from the function?

    Code:
    show_array(properties, &pk);
    This passes the address of the variable pk to show_array. The value of pk is the incorrect value returned from fill_array. In show_array,

    Code:
    for (pt = begin; pt != end; pt++) {
    you are setting pt to the address of the start of the array, then you are incrementing the address and testing it against end. end is the address of the pk variable in main. This has no relation to the array. So the for condition will never be met - hence an infinite loop!

    Also in show_array

    Code:
    cout << "Property #" << (pt + 1) << ": $";
    cout << pt << endl;
    You are first displaying the memory address of one value past pt and then displaying the memory address of pt??

    You have the same problem with revalue. Also in revalue, you are passing in the revaluation factor which you never use! You calculate a value called revalue which you then don't use and loose when the function returns!

    For show_array, why not something like this

    Code:
    void show_array(double array[])
    {
        for (int i = 0; i < Max; i++)
            cout << "Property #" << (i + 1) << ": $" << array[i] << endl;
    }
    Last edited by 2kaud; May 21st, 2013 at 03:19 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Apr 2013
    Posts
    42

    Re: return pointer

    i can be 1 through 5. And that part of the program works.

    I need to return a pointer.

  5. #5
    Join Date
    Apr 2013
    Posts
    42

    Re: return pointer

    I have rewritten the code and it still doesn't work, but should. Any advice?

    Code:
    #include <iostream>
    const int Max = 5;
    
    // function prototypes
    double fill_array(double ar[], int limit);
    void show_array(double * begin, double * end);
    void revalue(double r, double ar[], const double * begin, const double * end);
    
    int main()
    {
        using namespace std;
        double properties[Max];
    
        int pi = fill_array(properties, Max);
        show_array(properties, properties + pi);
        if (pi > 0)
        {
            cout << "Enter revaluation factor: ";
            double factor;
            while (!(cin >> factor))    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; Please enter a number: ";
            }
            revalue(factor, properties, properties, properties + pi);
            show_array(properties, properties + pi);
        }
        cout << "Done.\n";
        // cin.get();
        // cin.get();
        return 0;
    }
    
    double fill_array(double ar[], int limit)
    {
        using namespace std;
        double temp;
        //double * pt;
        int i;
        for (i = 0; i < limit; i++)
        {
            cout << "Enter value #" << (i + 1) << ": ";
            cin >> temp;
            if (!cin)    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; input process terminated.\n";
               break;
            }
            else if (temp < 0)     // signal to terminate
                break;
            ar[i] = temp;
        }
        //pt = (ar + i);
        //return *pt;
        return i;
    }
    
    // the following function can use, but not alter,
    // the array whose address is ar
    void show_array(double * begin, double * end)
    {
        using namespace std;
        const double * pt;
        for (pt = begin; pt != end; pt++)
        {
            cout << "Property #" << (pt + 1) << ": $";
            cout << pt << endl;
        }
    }
    
    // multiplies each element of ar[] by r
    void revalue(double r, double ar[], const double * begin, const double * end)
    {
        const double * pt;
        double revalue = 0;
        for (pt = begin; pt != end; pt++)
        {
            revalue = revalue * *pt;
        }
    }

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,384

    Re: return pointer

    Quote Originally Posted by FenixRising View Post
    I have rewritten the code and it still doesn't work, but should. Any advice?
    If the fill function needs to return a pointer to a double, declare the function prototype to return a double*.

    Not sure why you haven't changed this in your latest code after other folks have told you that you aren't returning a pointer to a double, but only a double.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,572

    Re: return pointer

    In show_array, try

    Code:
    cout << *pt << endl;
    pt is a pointer to a double, so to get the value to which it points you have to de-reference the pointer.

    Also,

    Code:
    cout << pt + 1
    shows the value of the memory address one double after the address contained in pt. If you want to display the property number, who will need to subtract the address of the start of the array.

    As fill_array now returns i which is an int, the function should be defined/declared to return an int.

    You're still got a problem in revalue though. You are not changing the values of properties. Revalue starts at 0 and anything multiplied by 0 stays at 0!
    Last edited by 2kaud; May 24th, 2013 at 07:40 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,434

    Re: return pointer

    Quote Originally Posted by FenixRising View Post
    I have rewritten the code and it still doesn't work, but should.
    Why do you believe it "should work"? Do you believe that something is wrong with the compiler, and that the compiler generated incorrect machine code?
    Any advice?
    Have you run the code in the debugger to see what the values of the variables are?
    I think part of the problem is the returned pointer from the fill function
    There is no need to "think" what the problem is. You are using Visual Studio, which has one of the best debuggers in the C++ world. All you need to do is debug your program using the debugger to see exactly what the values of the variables are and check the flow of your program.

    Regards,

    Paul McKenzie

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,132

    Re: return pointer

    I have no idea why you think fill returns a pointer. It's declared to return a double and the variable you return is an int. I don't even know why it should return a pointer. What's it supposed to return a pointer to?

  10. #10
    Join Date
    Apr 2013
    Posts
    42

    Smile Re: return pointer

    Hi,
    I have finished my exercise and I believe that it is correct.
    Concerning debugging my exercises, I am a complete novice. I am teaching myself all things C++. I have Microsoft Visual Studio Professional 2012. Now, I need a book or other source (but I prefer a book) to teach me how to use C++ with MS Visual Studio Professional 2012. I need ALL suggestions any of you have about anything C++ and VS. Please, I need a good book.

    Thanks

    Code:
    #include <iostream>
    const int Max = 5;
    
    // function prototypes
    double* fill_array(double ar[], int limit);
    void show_array(double * begin, double * end);
    void revalue(double r, double ar[], double * begin, double * end);
    
    int main()
    {
        using namespace std;
        double properties[Max];
    
        double* pt = fill_array(properties, Max);
        show_array(properties, pt);
        if (pt > 0)
        {
            cout << "Enter revaluation factor: ";
            double factor;
            while (!(cin >> factor))    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; Please enter a number: ";
            }
            revalue(factor, properties, properties, pt);
            show_array(properties, pt);
        }
        cout << "Done.\n";
        // cin.get();
        // cin.get();
        return 0;
    }
    
    double* fill_array(double ar[], int limit)
    {
        using namespace std;
        double temp;
        double * pt;
        int i;
        for (i = 0; i < limit; i++)
        {
            cout << "Enter value #" << (i + 1) << ": ";
            cin >> temp;
            if (!cin)    // bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
               cout << "Bad input; input process terminated.\n";
               break;
            }
            else if (temp < 0)     // signal to terminate
                break;
            ar[i] = temp;
        }
        pt = (ar + i);
        return pt;
        //return i;
    }
    
    // the following function can use, but not alter,
    // the array whose address is ar
    void show_array(double * begin, double * end)
    {
        using namespace std;
        const double * pt;
        int i = 0;
        for (pt = begin; pt != end; pt++)
        {
            i++;
            cout << "Property #" << i << ": $";
            cout << *pt << endl;
        }
    }
    
    // multiplies each element of ar[] by r
    void revalue(double r, double ar[], double * begin, double * end)
    {
        double * pt;
        //double revalue = 0;
        for (pt = begin; pt != end; pt++)
        {
            *pt = r * *pt;
        }
    }

  11. #11
    Join Date
    Apr 1999
    Posts
    27,434

    Re: return pointer

    Quote Originally Posted by FenixRising View Post
    Hi,
    I have finished my exercise and I believe that it is correct.
    Concerning debugging my exercises, I am a complete novice.
    That really doesn't matter. When you write a program, you're required to be able to debug what you wrote. Just writing code, running it, seeing that it doesn't work, and then asking one of us to debug the code is not how you learn a programming language or how to properly write programs. Debugging is a requirement, not an option, when it comes to learning how to program. IMO it has to be taught at the same time you write your first line of code.

    There is no requirement that on the first go, your program works. On very rare occasions (unless the program is very simple) does someone write a program that works the first time. When the program doesn't work, the requirement is that you're able to figure out for yourself why the code you wrote doesn't perform correctly (I emphasize you to make a point).

    When you write a program, you are supposed to know what every line is supposed to do, what every function is supposed to perform, what the exact flow is supposed to be, etc. If you write a program by just throwing stuff at the compiler without knowing what everything is supposed to do, then that is not learning how to write a program.

    When the program doesn't perform as required, then you know what was supposed to happen, because again, you had a plan and you put that plan to code. That is when you start to debug the program to see where the program diverges from your plans. Then you make the requisite changes to fix the problem, or ask further questions to someone saying "hey, why is it when my pointer gets incremented by x did this happen" or "why is it that when I returned a pointer to this local, everything just disappeared" or some other focused question.

    To debug a program there is a Debug menu. All you need to do is hit F10 and keep hitting F10 after your program is built successfully. That is the easiest way to see at a birds-eye view, what a debugger does. The debugger allows you to run your program a single "step" at a time under your control. You are able to watch the value of variables, check the flow of the program, with again, the full knowledge of what the program is supposed to do and how you planned to do it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 26th, 2013 at 10:42 PM.

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,572

    Re: return pointer

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Apr 2013
    Posts
    42

    Re: return pointer

    Thanks to everyone that gave me their input, thank you.

  14. #14
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,997

    Re: return pointer

    Quote Originally Posted by Paul McKenzie View Post
    There is no requirement that on the first go, your program works. On very rare occasions (unless the program is very simple) does someone write a program that works the first time.
    Whenever I write a piece of code, and when trying it for the first time it seems to work perfectly... This instantly makes me wonder what I did wrong to make it work right from the start. That alone deserves further investigation and debugging just to make sure

  15. #15
    Join Date
    Apr 1999
    Posts
    27,434

    Re: return pointer

    Quote Originally Posted by OReubens View Post
    Whenever I write a piece of code, and when trying it for the first time it seems to work perfectly... This instantly makes me wonder what I did wrong to make it work right from the start. That alone deserves further investigation and debugging just to make sure
    Same here. If a program works the first time, I need to look at it further to see if I missed anything.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center