Thread: pointers in function declaration

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    pointers in function declaration

    To clear up some confusion I have over pointers in the function declaration, I tried writing a program.

    Here the program is:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class number
    {
    public:
    	number(int initNum);
    	~number() {}
    	int GetA() const {return a;}
    	void SetA(int x) {a=x;}
    private:
    	int a;
    };
    
    number::number(int initNum)
    {
    	a=initNum;
    }
    	
    number* changer(number *theNum);
    
    int main()
    {
    	number test(2);
    
    	cout<<test.GetA()<<"\n";
    	changer(&test);
    	cout<<test.GetA()<<"\n";
    	return 0;
    
    }
    
    number* changer(number *theNum)
    {
    	theNum->SetA(5);
    	return theNum;	//I realize that in this program, this line does nothing
    }


    Bascially, I don't fully understand this line:
    number* changer(number *theNum)
    ^^^^^^
    | | | | | |
    that mostly

    In my program, when I return theNum, it is sending back an memory address correct? How does the number* part modify this? Does it dereference the memory address that I send it?

    Hopefully that made sense...
    Thanks for any help,
    --rdk

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    number * : This is the return type of the function. You are sending back a pointer (yes, a memory address) to a variable of type 'number'. It's the * that tells the compiler to expect a pointer.

    changer : just the name of the function

    (number *theNum) : the parameters. This allows the person that uses or calls this function to pass required information to the code inside. Again, it is a pointer to a number, and can be referred to in your code as 'theNum'.

    Is this clearer?

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    -edit-
    beaten by sean, heh

    Basically what is happening is you're returning a number* from the function.

    Just as you'd do:
    Code:
       int myFunc(int var);
       
       int myFunc(int var)
       {
       	var++;
       	return var;
       }
    to return an int value


    This works pretty much the same way. In the function above, you pass an int value, it increases it, and then returns the new int value.

    The main difference is that you're passing a pointer to a class, so the actual return value in this case won't be any different than the value you passed.
    Last edited by jverkoey; 08-18-2004 at 03:41 PM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    Thanks for the help. I understand it now. It was one of those stupid mistakes that you can't believe you made... Oh well, onward I march with learning c++.

    Thanks again,
    --rdk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM