Thread: pass by reference .... HELP!!

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by 7stud
    I understand joni's point. Both functions are called the same way. So the point is: if all arguments are copied and two functions are called with the same argument, yet the two functions end up with two different things, then there is something dissimilar about the way the argument is being passed.
    But they aren't called the same way. Again, the syntax is hidden from you to make your life simpler. In the case of the pass-by-reference example, even though you say function(str) just as in the pass-by-copy example, that call will get translated to function(&str) for you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #17
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by tretton
    Code:
    void changeMe(char param[])
    {
        param = "World!";
    }
    I'm not an expert, but I would say that you are just telling the local pointer param to point to the read-only string "World!". This pointer is discarded when the function exits, and since the function doesn't actually do anything, str in main remains unchanged.

    Remember that everything is passed by value, so the changeMe() only gets a copy of the pointer str. If you inside the function want to alter what both str and param points to, use something like strcpy(param, "World!").

    -tretton
    How does strcpy() change what is at str though, I had people explaining it to me on IRC before but I still don't really understand it.

    I don't see why strcpy(param, "World!");
    is different from param = "World!";

    Is the first parameter of strcpy() setup so it is passed like &param, if that is the case, wouldn't the function that calls strcpy() which is changeMe() wouldn't that have to be setup to be &param aswell?

    Or can you pass an array to a function like (array[]) and then inside that function pass it to another function like (&array).

    Sorry 2 questions there, the first is the most important, that I don't understand.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    param is a copy of a pointer to somewhere in memory. When you use param = "World!" you are pointing the copy at a different place in memory. The original still points to "Hello ".

    However, if you use strcpy(param, "World!"), you are not pointing param to a different place. Instead, you are copying the characters from "World!" to the memory location pointed at by param. Since param points to the same memory as the original pointer passed to the function does, the original pointer also points at those characters after strcpy.

    So in the first instance, nothing changes what the original pointer pointed to, but in the second strcpy did.

  4. #19
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by Daved
    param is a copy of a pointer to somewhere in memory. When you use param = "World!" you are pointing the copy at a different place in memory. The original still points to "Hello ".

    However, if you use strcpy(param, "World!"), you are not pointing param to a different place. Instead, you are copying the characters from "World!" to the memory location pointed at by param. Since param points to the same memory as the original pointer passed to the function does, the original pointer also points at those characters after strcpy.

    So in the first instance, nothing changes what the original pointer pointed to, but in the second strcpy did.
    Thank you

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How does strcpy() change what is at str though, I had people explaining it to me on IRC before but I still don't really understand it.
    First, you have to realize that when you send an argument to a function, the parameter variable is created and then the argument is assigned to the parameter variable. In other words, the argument is stored in the parameter variable.

    Second, whenever you pass the name of an array to a function, the address of the array is actually what is passed to the function.

    Third, whenever the compiler encounters a string literal(i.e. anything between double quotes), like "World!", it slaps a '\0' on the end of it, stores it in memory somewhere, and returns the address. Additionally, the string is const, so you cannot change it. For instance, this doesn't work:
    Code:
    char* str = "Hello";
    str[0] = 'a';
    It will compile without error, but your program will crash. Digressing a bit further, this does work:
    Code:
    char str[] = "Hello";
    str[0] = 'a';
    That's because the first line causes an internal strcpy() to be performed. As before, the compiler takes the string literal "Hello" and slaps a '\0' onto the end and stores it in memory somewhere, but in this case the compiler strcpy()'s the string to the address of the newly created char array str. Furthermore, the copy is not const like the original, and it can be modified. Note: there are two copies of "Hello" in memory.

    In the example in the post you are asking about, str is an array and it is passed to the function. Therefore, the address of str is passed to the function, and it gets stored in the variable param. strcpy() is a function that says to copy one string into another string. In particular, strcpy(param, "World!") says to copy "World!" to the address stored in param. The address stored in param is the address of str, so "World!" gets copied to str's address. After the copying is done, there are two copies of "World!" in memory: one is located at str, and another is located elsewhere in memory(note: the string located at str is modifiable, while the other string is const and can't be modified).
    I don't see why strcpy(param, "World!");
    is different from param = "World!";
    The statement:
    Code:
    param = "World!"
    is executed like this: first, the compiler treats all string literals the same, so it slaps a '\0' onto the end of "World!" and stores it in memory. Then the compiler returns the address of where it stored the string, which then gets assigned to param. In C++, when you assign a value to a variable, it overwrites what was previously assigned to that variable, e.g.:
    Code:
    int i = 10;
    i = 20;
    cout<<i<<endl;
    Prior to the assignment, param contained the address of str. After the assignment param contains the address of "World!". str is wholly unaffected by what is assigned to param. Another way of looking at it is: before the assignment param and str pointed to the same place in memory. So, if you use param to change what is stored at that address, then you will change what str points to. That's what strcpy() does. However, assigning a different address to param does nothing to str, it just makes param point elsewhere. Here is a diagram of what is happening:

    Code:
             "Hello\0"                   
             / \
            /   \
           /     \
        str      param
    
    strcpy(param, "World!");
    
             "World!\0" (const)
    
             "World!\0"  (modifiable)                     
             / \
            /   \
           /     \
        str      param
    Code:
             "Hello\0"                   
             / \
            /   \
           /     \
        str      param
    
    param = "World!";
    
             "Hello\0"     "World!\0" (const)       
             /             /
            /             /
           /             /
        str        param
    Last edited by 7stud; 02-15-2006 at 01:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 02:23 PM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 12:14 PM