Pointer for Beginner

This is a discussion on Pointer for Beginner within the C++ Programming forums, part of the General Programming Boards category; Consider this: Code: void function1() { int variable = 5; function2(&variable); } void function2 (int *var) { *(var) = 10; ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    25

    Question Pointer for Beginner

    Consider this:
    Code:
    void function1()
    {
    int variable = 5;
    function2(&variable);
    }
    
    
    void function2 (int *var)
    {
    *(var) = 10; //Function1.variable now is 5
    return;
    }
    My question is: Instead of using "&variable" and "*var" as parameters, couldnt we just use "variable" and "var"?
    What is the point of using pointers in a function?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Why don't you test? For example, you could print the value of variable after calling function2, and see if it is changed with the use of a pointer, and then without the use of a pointer.

    By the way, we would more typically use a reference instead of a pointer parameter here.
    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

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    49
    Try it and see what happens, you'll learn things that way. Which variable is being used where?

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by Fossaw View Post
    Try it and see what happens, you'll learn things that way. Which variable is being used where?
    I tested it and it gives me the expected output but why use pointers or references? we could simply use regular variables.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Greek_89
    I tested it and it gives me the expected output but why use pointers or references? we could simply use regular variables.
    Ah, that means that your expectations are not the same as ours. Let's make the example more explicit:
    Code:
    #include <iostream>
    
    void foo(int& var)
    {
        var = 10;
    }
    
    int main()
    {
        int variable = 5;
        foo(variable);
        std::cout << variable << std::endl;
    }
    I expect the above program to print 10 when run, and it does. Since you claim that "we could simply use regular variables", change var to be an int instead of an int&, then compile and run. Do you still get 10 printed?
    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

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by laserlight View Post
    Ah, that means that your expectations are not the same as ours. Let's make the example more explicit:
    Code:
    #include <iostream>
    
    void foo(int& var)
    {
        var = 10;
    }
    
    int main()
    {
        int variable = 5;
        foo(variable);
        std::cout << variable << std::endl;
    }
    I expect the above program to print 10 when run, and it does. Since you claim that "we could simply use regular variables", change var to be an int instead of an int&, then compile and run. Do you still get 10 printed?
    OK. im tryin to give an explanation about the output: So we initialize "variable" with the value "5", then using the foo(variable), we pass INTO the function "5", the parameter is "&var" so it shows the value "5"...but then "var" gets the value "10" so the output will be "10".. but WHY if i take off the "&" my output is "5"?..i cannot explain it still..

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Greek_89
    we pass INTO the function "5", the parameter is "&var" so it shows the value "5"
    Not quite: the parameter is named var, of type int& (i.e., reference to int). Since this is pass by reference, not pass by value, you do not pass 5, i.e., the value of variable as the argument: you pass the variable variable as the argument.

    Quote Originally Posted by Greek_89
    but then "var" gets the value "10" so the output will be "10"
    Since var is a reference, it is an alias of the argument, i.e., it is an alias for variable. Changing the value of var changes the value of variable.

    Quote Originally Posted by Greek_89
    but WHY if i take off the "&" my output is "5"?
    Removing the "&" makes var into a value parameter. Hence it is not an alias of the argument, and indeed it stores a copy of the argument's value. As such, changing var has no effect on variable.

    You can repeat this reasoning for the use of a pointer, except that this time it would be *var that is an alias for variable.
    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

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    there are two ways that you pass things to functions: pass by value, and by reference. You need to understand what these mean, right? Here is an example:
    Code:
    struct tes{
          int x, y, z;
    };
    void foocopy(tes var){
        // do something here, bla bla
    }
    void foorefer(tes& var){
     // do something here, bla bla
    }
    When you call foocopy(tes var) a brand new copy of the struct tes is made when the function is called, so if you did this:

    tes v;
    v.x=v.y=v.z=6;
    foocopy(v);

    no matter what you do to the variable inside foo, v will never change!, Why? Because you passed it by value, which means a NEW COPY is made when the function is called and that is what the function works with.... a copy!

    BUT, if you do this :

    tes v;
    v.x=v.y=v.z=6;
    foorefer(v);

    A new copy is NOT made and anything you do inside the function foorefer ACTUALLY modifies v.


    This is very usefull because sometimes you do not want to MODIFY the variable passed to a function, and sometimes you do. There is also SPEED considerations here. Each time a variable is COPIED, there is extra work. Passing something by reference is the fastest, because no copies are made or anything.

    You need to know what POINTERS actually are first!

    ANY pointer, of any type, i.e. int*, float*, tes* ANY pointer!!!! is really a memory address, or more Precisely, A NUMBER.

    when you typed this in your code.............

    function2(&variable);


    the &variable will Get the Memory Address of variable and pass that to function2. pointers are memory addresses, mmkay?

    so, when function2 runs, it receives the memory address ( also known as a pointer), called var. SINCE you did not pass by reference, A COPY IS MADE of the pointer, or memory address that was passed.

    Well, what the hell does a * do then? A * in front of a pointer type GETS the value stored at the memory address that it holds. In this case, you are telling the compiler to place the value of 10 at the memory address held by var. var is holding the memory address of variable, which is why it changes!
    Last edited by smasherprog; 05-04-2010 at 11:47 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by smasherprog
    There is also SPEED considerations here. Each time a variable is COPIED, there is extra work. Passing something by reference is the fastest, because no copies are made or anything.
    Not necessarily: the reference might be implemented using a pointer, and if so there will be the cost of copying a pointer. If the cost of copying the object is the same as that of copying a pointer, then it may actually be very slightly slower to pass by reference.
    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

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by laserlight View Post
    Not quite: the parameter is named var, of type int& (i.e., reference to int). Since this is pass by reference, not pass by value, you do not pass 5, i.e., the value of variable as the argument: you pass the variable variable as the argument.


    Since var is a reference, it is an alias of the argument, i.e., it is an alias for variable. Changing the value of var changes the value of variable.


    Removing the "&" makes var into a value parameter. Hence it is not an alias of the argument, and indeed it stores a copy of the argument's value. As such, changing var has no effect on variable.

    You can repeat this reasoning for the use of a pointer, except that this time it would be *var that is an alias for variable.

    great explanation..now i see a little bit clearer..so what i get from that is that pointers are useful for good memory use..right?

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    They are not useful, they're essential.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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