Like Tree2Likes
  • 1 Post By MutantJohn
  • 1 Post By laserlight

[jumping into c++] Trouble on pointer chapter.

This is a discussion on [jumping into c++] Trouble on pointer chapter. within the C++ Programming forums, part of the General Programming Boards category; Hello, I'd like some help with one of the practice problems given in the chapter concerning pointers. Write a function ...

  1. #1
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10

    Question [jumping into c++] Trouble on pointer chapter.

    Hello, I'd like some help with one of the practice problems given in the chapter concerning pointers.

    Write a function that takes two input arguments and provides two separate results to the caller, one
    that is the result of multiplying the two arguments, the other the result of adding them. Since you can
    directly return only one value from a function, you'll need the second value to be returned through a
    pointer or reference parameter.

    So I don't want answers just handed to me, but i'm having trouble grasping this situation. so far my code looks like this.

    #include <iostream>
    using namespace std;
    /*4. Write a function that takes two input arguments and provides two separate results to the caller, one
    that is the result of multiplying the two arguments, the other the result of adding them. Since you can
    directly return only one value from a function, you'll need the second value to be returned through a
    pointer or reference parameter.
    Code:
    #include <iostream>
    using namespace std;
    /*4. Write a function that takes two input arguments and provides two separate results to the caller, one
    that is the result of multiplying the two arguments, the other the result of adding them. Since you can
    directly return only one value from a function, you'll need the second value to be returned through a
    pointer or reference parameter.
    */
    void doMath(int i,int j,int resultOne,int resultTwo);
    int main()
    {
        int resultOne = 0;
        int resultTwo = 0;
        int i = 5;
        int j = 10;
        doMath(i,j,resultOne,resultTwo);
        cout << resultOne;
        cout << resultTwo;
    }
    void doMath(int i, int j,int *resultOne,int *resultTwo)
    {
        *resultOne = i * j;
        *resultTwo = i + j;
    }
    I get an error from my compiler using this, which reads "undefined reference to doMath(int, int, int, int)'|"

    I believe it is because the 3rd and 4th parameter require pointers, and when i'm calling the function, resultOne and resultTwo are not initialized.

    Any thoughts or advice? Thanks.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,067
    It's because in your initial declaration, resultOne and resultTwo were delcared as ints whereas in where you actually implement doMath, you use them as pointers.

  3. #3
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Thanks for the reply, I changed resultOne (line 11) and resultTwo (line 12) to be pointers set to 0. However I am still getting the same error.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,067
    It's because you're also not declaring resultOne and resultTwo as pointers in your code too.

  5. #5
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Ok, line 8 has been modified so that resultOne and resultTwo are pointers. Now i'm getting some fresh new errors.


    Code:
    In function 'int main()':|
    error: invalid conversion from 'int' to 'int*'|
    error:   initializing argument 3 of 'void doMath(int, int, int*, int*)'|
    error: invalid conversion from 'int' to 'int*'|
    error:   initializing argument 4 of 'void doMath(int, int, int*, int*)'|
    All of which occur line 15

    just to give an overview, it looks like this now
    Code:
    #include <iostream>
    using namespace std;
    /*4. Write a function that takes two input arguments and provides two separate results to the caller, one
    that is the result of multiplying the two arguments, the other the result of adding them. Since you can
    directly return only one value from a function, you'll need the second value to be returned through a
    pointer or reference parameter.
    */
    void doMath(int i,int j,int *result1,int *result2);
    int main()
    {
        int *resultOne =0;
        int *resultTwo =0;
        int i = 5;
        int j = 10;
        doMath(i,j,*resultOne,*resultTwo);
        cout << resultOne;
        cout << resultTwo;
    }
    void doMath(int i, int j,int *resultOne,int *resultTwo)
    {
        *resultOne = i * j;
        *resultTwo = i + j;
    }
    Thank you for your help so far also, its really appreciated.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,067
    What do you think is odd about this line of code you wrote :
    Code:
    int *resultOne = 0; // try something like 0x0 (or NULL)
    I don't know how much you actually know or have read about pointers but a pointer is a variable whose value is an address in memory. Ironically enough, it is a number, just like 0 but it's written in hexadecimal. You can get the actual number by recasting it as size_t, I think.

    For example, look at this code for a basic explanation :
    Code:
    int x = 3; // declare an int the ordinary way
    int *p = &x; // declare a pointer and initialize it to the address of x
    
    cout << "The value at " << p << " is equal to : " << *p << endl;
    In the cout line, the difference between p and *p is that p is the address in your computer's memory. *p is what is stored at that address. Normally, an int is 4 bytes. p points to a block of memory 4 bytes wide and the value of those 4 bytes is returned by *p.

    I think I explained that correctly. As always, independent reading is always encouraged.

  7. #7
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Your clarification helps alot. It's mainly that i'm trying to wrap my head around pointers. If I may ask quickly, is there a reason to say null instead of 0? I know you should always set them to avoid random errors and bugs, but I don't recall the book mentioning the difference. Also my program compiles correctly now. The fun part is when I run it, I get a crash.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,719
    Quote Originally Posted by CrimsonMagi
    is there a reason to say null instead of 0?
    Refer to Stroustrup's answer to the FAQ: Should I use NULL or 0?

    Quote Originally Posted by CrimsonMagi
    The fun part is when I run it, I get a crash.
    Those pointers don't point to anything. Rather, you should write:
    Code:
    int resultOne = 0;
    int resultTwo = 0;
    int i = 5;
    int j = 10;
    doMath(i, j, &resultOne, &resultTwo);
    cout << resultOne;
    cout << resultTwo;
    Now, resultOne and resultTwo are ints, not pointers, so you pass their addresses to doMath. Note that because they aren't actually optional in doMath, we would normally use reference parameters instead of pointer parameters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,067
    Well, technically NULL is 0, just written in a different way. That's why I suggested using 0x0 because NULL is 0x0.

    If you print out a pointer normally, you should see something like 0x123ef89a. This is a hexadecimal number 0-9 is well, 0-9 and a is 10, b is 11, c is 12, d is 13, e is 14 and f is 15 so that's all 16 numbers (hence hexadecimal which translates to 16). It's a way of writing an address using 4 bit blocks as 4 bits represent 16 values (2^4 possible binary strings = 16 unique strings).

    So for pointers, initializing them to 0x0 or NULL is the same thing. I prefer 0x0 because it makes me feel fancy.

    Also, this line :
    Code:
    doMath(i,j,*resultOne,*resultTwo);
    You're dereferencing your pointers when you should just be passing them as is so use
    Code:
    doMatch(i, j, resultOne, resultTwo);
    You should, however, dereference your pointers when you're printing the results otherwise you'll just print the values of the pointers which is an address.

    Edit: And Laser's right, your pointers don't actually point to anything if initialize them to 0. They need to point to a valid memory address to be used. Duh. I'm tired T_T
    CrimsonMagi likes this.

  10. #10
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Thank you both very much for the help. The link you gave me cleared things up on the null vs 0. I'm just having a hard time getting pointers, but i'm getting it more and more. So now from my understanding, pointers don't take the value of int i, it takes the literal memory address which is why & is used.

    Thanks again, I really appreciate the help given. This forum doesn't seem to have +rep. So..
    Have a cookie
    Name:  cookie.jpg
Views: 109
Size:  66.6 KB

  11. #11
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Don't feel bad, I'm tired too. Setting resultOne and resultTwo to be pointers was a dumb idea. So no sweat

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,719
    Quote Originally Posted by MutantJohn
    What do you think is odd about this line of code you wrote :
    Code:
    int *resultOne = 0; // try something like 0x0 (or NULL)
    By itself, there is nothing odd about that line. I don't recommend writing it as 0x0: it looks odd to me as I don't recall ever seeing C++ code that uses 0x0 as a null pointer constant. Usually, if 0x0 is used, it is because some bitwise arithmetic is involved.

    Quote Originally Posted by MutantJohn
    I don't know how much you actually know or have read about pointers but a pointer is a variable whose value is an address in memory. Ironically enough, it is a number, just like 0 but it's written in hexadecimal.
    I usually see addresses represented in hexadecimal when printed with C++ I/O streams, but I don't believe that that is required.

    Quote Originally Posted by MutantJohn
    You can get the actual number by recasting it as size_t, I think.
    If you want to see the "actual number", then just print it.
    CrimsonMagi likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,067
    Quote Originally Posted by laserlight View Post
    By itself, there is nothing odd about that line. I don't recommend writing it as 0x0: it looks odd to me as I don't recall ever seeing C++ code that uses 0x0 as a null pointer constant. Usually, if 0x0 is used, it is because some bitwise arithmetic is involved.


    I usually see addresses represented in hexadecimal when printed with C++ I/O streams, but I don't believe that that is required.


    If you want to see the "actual number", then just print it.
    Huh, then my bad.

    You're right though. C++ I/O streams use hexadecimal so I just assumed it was the thing to do and I guess I just got used to it.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Since you can directly return only one value from a function, you'll need the second value to be returned through a
    pointer or reference parameter.
    The way I interpret that statement is that the assignment is expecting the function to return a value and have another value set inside the function that gets passed back to the caller through a reference/pointer parameter. Your code above returns nothing and uses two reference/pointer parameters to pass back both altered values to the caller.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by MutantJohn View Post
    Well, technically NULL is 0, just written in a different way. That's why I suggested using 0x0 because NULL is 0x0.
    0 == 0x0 == NULL
    If you are going to suggest anything, I would recommend you suggest using nullptr, which is a keyword introduced in C++11. Basically, it is the new 0 or NULL for pointers. But this time around it's not an int, so it doesn't defy the type system or produce weird behavior:

    Code:
    void foo(int);
    void foo(int*);
    
    foo(NULL); // Calls foo(int), NOT foo(int*).
    foo(nullptr); // C++11 - calls foo(int*), as expected.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe! (Noob Coder)_Problem 3 Chapter 7 Jumping into C++
    By Jonathan002 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2013, 12:28 AM
  2. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  3. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  4. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  5. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 04:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21