Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Passing an array ( to a function ) by value

smallmoovzsmallmoovz Member Posts: 7
[b][red]This message was edited by smallmoovz at 2002-7-31 0:20:53[/red][/b][hr]
Usually, a variable is passed from a calling function to a called function by value if it is required that the value of the variable in the calling function is not affected ( changed ) by the operations carried out upon that variable in the called function. Arrays however, are passed by reference and hence any changes made to the array in the called function affect the array in the calling function too. A couple of ways to prevent this from happening :
1) Copy the array to another identical array in the calling function and then pass the copy to the called function.
2) Pass the array to the called function, element-by-element ( array elements are passed by value ).
Is there a better and more efficient way to pass an entire array by value from a calling function to a called function? If yes, pray what is it?



Comments

  • whoiewhoie Member Posts: 672
    : Usually, a variable is passed from a calling function to a called function by value if it is required that the value of the variable in the calling function is not affected ( changed ) by the operations carried out upon that variable in the called function. Arrays however, are passed by reference

    Loosely defined, yes. Technically, no they aren't. Pass by reference means something a little different in C++. In C and C++, arrays are passed by a pointer to their first element. That isn't strictly pass by reference. But I know what you mean ;).


    : and hence any changes made to the array in the called function affect the array in the calling function too. A couple of ways to prevent this from happening :
    : 1) Copy the array to another identical array in the calling function and then pass the copy to the called function.

    Correct.


    : 2) Pass the array to the called function, element-by-element ( array elements are passed by value ).

    How exactly do you mean to do this? Variable parameter lists? I simply don't understand what you mean here. If I have an array of 250 elements, are you saying to pass each one individually?


    : Is there a better and more efficient way to pass an entire array by value from a calling function to a called function? If yes, pray what is it?

    Well, it's never really "efficient" to pass an array by value. That's the whole reason for passing the pointer in the first place. But if you want an easy way to do so, then wrap the array in a structure. Now it will behave like a first-class type, because structs can be passed and returned by value.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • tsagldtsagld Member Posts: 621
    Sorry Will, but your solution to pass a struct doesn't help, I believe.
    The passed structure still contains a pointer to the first element of the original array.
    Passing the struct by value prevents you from modifying the pointer, but not from modifying the memory the pointer points to. So you're still able to modify the array in the called function.
    I will work with a fixed-size array, but you won't need a struct anyway in that case.
    Didn't test this, but it makes sense, doesn't it?

    : : Usually, a variable is passed from a calling function to a called function by value if it is required that the value of the variable in the calling function is not affected ( changed ) by the operations carried out upon that variable in the called function. Arrays however, are passed by reference
    :
    : Loosely defined, yes. Technically, no they aren't. Pass by reference means something a little different in C++. In C and C++, arrays are passed by a pointer to their first element. That isn't strictly pass by reference. But I know what you mean ;).
    :
    :
    : : and hence any changes made to the array in the called function affect the array in the calling function too. A couple of ways to prevent this from happening :
    : : 1) Copy the array to another identical array in the calling function and then pass the copy to the called function.
    :
    : Correct.
    :
    :
    : : 2) Pass the array to the called function, element-by-element ( array elements are passed by value ).
    :
    : How exactly do you mean to do this? Variable parameter lists? I simply don't understand what you mean here. If I have an array of 250 elements, are you saying to pass each one individually?
    :
    :
    : : Is there a better and more efficient way to pass an entire array by value from a calling function to a called function? If yes, pray what is it?
    :
    : Well, it's never really "efficient" to pass an array by value. That's the whole reason for passing the pointer in the first place. But if you want an easy way to do so, then wrap the array in a structure. Now it will behave like a first-class type, because structs can be passed and returned by value.
    :
    :
    : HTH,
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • whoiewhoie Member Posts: 672
    : Sorry Will, but your solution to pass a struct doesn't help, I believe.

    No need to apologize, but I appreciate the consideration :).


    : The passed structure still contains a pointer to the first element of the original array.

    Perhaps an illustration will make this more clear. For example:
    [code=ffffff]
    struct array_byvalue {
    char text[100];
    };
    [/code]
    There is no pointer there at all. All 100 bytes of this structure can be passed and returned by value.

    : Passing the struct by value prevents you from modifying the pointer, but not from modifying the memory the pointer points to. So you're still able to modify the array in the called function.

    No, because arrays are not pointers. Here is a sample program using the above structure to show what I mean:
    [code=ffffff]
    #include
    #include

    struct array_byvalue hoie_cat(struct array_byvalue arr)
    {
    strcat(arr.text, " Hoie");
    return arr;
    }

    int main(void)
    {
    struct array_byvalue firstname = {"Will"};
    struct array_byvalue fullname = hoie_cat(firstname);

    printf("First name: %s
    ", firstname.text);
    printf("Full name: %s
    ", fullname.text);

    return 0;
    }
    [/code]

    Output:

    C:Csrc>foo
    First name: Will
    Full name: Will Hoie


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • PeterTheMasterPeterTheMaster Member Posts: 636

    : ...efficient way to pass an entire array by value...

    i call this a good example for an oxymoron.

    tsagld:
    : will work with a fixed-size array

    i think in the strict sense of types there are not non-fixed-size arrays.

    : The passed structure still contains a pointer to the first element of the original array.

    and as you should have learned from a lot of earlier posts: be careful with the words pointer and array.
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ____ < http://forcoder.org /> free video tutorials and ebooks about // C++, Objective-C, Delphi, R, Python, Visual Basic .NET, Scratch, C, Visual Basic, Java, PHP, Perl, Ruby, PL/SQL, Go, C#, MATLAB, Assembly, JavaScript, Swift SAS, Clojure, Bash, Alice, COBOL, ABAP, Erlang, Rust, Lua, Julia, Scheme, VBScript, Scala, Dart, Awk, Fortran, Prolog, Logo, LabVIEW, FoxPro, Apex, Lisp, D, Hack, F#, ML, Transact-SQL, Ada, Kotlin, Crystal // ____________

Sign In or Register to comment.