Thread: I need help with pointers (& and *) and functions

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    I need help with pointers (& and *) and functions

    I'm trying to return the pointer to the variable that has the bigger value using pointers & and *. Below is the source code for my main.cc, functions.cc, and functions.h files. What did I do wrong here? I have also posted the error that it gave me.


    ERROR STATEMENT:
    functions.cc:9:10: error: cannot initialize return object of type 'int *' with
    an lvalue of type 'int'
    return *a;
    ^~
    functions.cc:11:10: error: cannot initialize return object of type 'int *' with
    an lvalue of type 'int'
    return *b;
    ^~
    2 errors generated.
    make: *** [functions.o] Error 1






    main.cc
    Code:
    #include <string>
    #include <cstdio>
    #include <vector>
    #include "functions.h"
    using namespace std;
    
    
    int main( int argc, char** argv ) {
    
    
        int x = 100;
        int y = 200;
        int result = 0;
    
    
        // find out which of the two variables (x and y) is the largest
        pointerToMax(&x, &y);
    }






    functions.cc
    Code:
    #include <string>
    #include <cstdio>
    #include <vector>
    #include "functions.h"
    using namespace std;
    
    
    int* pointerToMax(int* a, int* b) {
        if(a > b) {
            return *a;
        } else {
            return *b;
        }
    } // end of pointerToMax






    functions.h
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    
    int* pointerToMax(int* a, int* b);
    
    
    #endif

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's examine the error message:
    Code:
    functions.cc:9:10: error: cannot initialize return object of type 'int *' with
    an lvalue of type 'int'
    return *a;
    ^~
    Notice that it tells you which file, line, and column the error was detected. It also provides the corresponding code snippet. This is not always the point where the error lies, but if the error is not at this point, then it probably lies somewhere above it. In this case, the error does indeed lie at this point.

    You declared the pointerToMax function as returning an int*, i.e., pointer to int. But *a is an int, not a pointer to int. Therefore, you have a type mismatch. What you probably want to do is to return a instead.

    The same goes for the next error message: it is a mirror of this one but with *b vs b.

    By the way, you have a bug on this line:
    Code:
    if(a > b) {
    Think about what you actually want to compare versus what you are comparing with the code that you wrote.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Muting from functions to pointers to functions
    By Fiskker in forum C Programming
    Replies: 2
    Last Post: 06-18-2015, 12:57 PM
  2. Pointers and functions help
    By figarojones in forum C Programming
    Replies: 10
    Last Post: 11-29-2012, 03:47 PM
  3. pointers and functions
    By beebee in forum C Programming
    Replies: 1
    Last Post: 11-25-2012, 06:50 PM
  4. Pointers and functions help
    By figarojones in forum C Programming
    Replies: 5
    Last Post: 11-24-2012, 11:21 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread