Thread: Passing an address to a function

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Passing an address to a function

    Lets say we have a variable we want to change through a function.

    What is the best way to do this, is one method better than another?

    Code:
    int number;
    
    void ChangeNumber(int &number)
    {
    
        // change number
    
    }
    Is there a better way to do that.
    What is C++?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You should prefer using references unless it's possible to pass a "nothing" object, in which case you have no choice but to use pointers because there's no such thing as a null reference.
    My best code is written with the delete key.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Just to make sure I understand.

    Code:
    void ChangeNumber(int *number)
    {
    
        // change number
    
    }
    I am at a point in programming where I have just recently started to learn the language. I have used C++ for about 2-3 years but I just toyed with it. I am just now getting into it and learning how/why it works.

    So the & operator gets the address-of and * creates a reference.

    Now, what is it actually doing. I understand that the & operator passes the memory location of the variable. But what does * do?
    What is C++?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    & is the address-of when used with a variable
    & means its a reference when used with parameters and variable declartions
    * means its a pointer when used with parameters and variable declartions
    * means to deference a pointer when used with a variable

    Code:
    int a = 10;
    int *b; // B is a pointer
    b = &a;  // &a is the address of a
    int &c = a; // &c means c is a reference to a
    *b = 10; // go to the location b points to and change its value to 10

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So the & operator gets the address-of and * creates a reference.
    No, not really. Operators in C++ are overloaded to do different things based on the context. When used in a declaration, the ampersand means that you are creating a reference:
    Code:
    int i = 5;
    int& ri = i; // Any changes to ri reflect i
    When used as a value, the ampersand takes the address of an object:
    Code:
    int i = 5;
    
    cout<< &i <<endl; // Prints i's address
    It also does other things, such as bitwise AND, but we'll ignore that for now.

    The asterisk in a declaration creates a pointer (ie. a variable that holds an address):
    Code:
    int i = 5;
    int *pi = &i; // pi "points to" i
    When used in an expression, the asterisk means to dereference a pointer so that you are looking at the contents of the address rather than the address itself:
    Code:
    cout<< *pi <<endl; // Prints 5 because pi points to i
    My best code is written with the delete key.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thank you both for the info.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM