Thread: pointers in function declaration

  1. #1
    Registered User
    Join Date
    Aug 2004

    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:
    #include <iostream>
    using namespace std;
    class number
    	number(int initNum);
    	~number() {}
    	int GetA() const {return a;}
    	void SetA(int x) {a=x;}
    	int a;
    number::number(int initNum)
    number* changer(number *theNum);
    int main()
    	number test(2);
    	return 0;
    number* changer(number *theNum)
    	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,

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    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
    University of Waterloo
    beaten by sean, heh

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

    Just as you'd do:
       int myFunc(int var);
       int myFunc(int 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
    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,

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