Thread: passing arrays to functions

  1. #1

    Question passing arrays to functions


    I'm having trouble passing an array to a function. Arrays are passed "call by value" which means the array elements are not copied when I pass them.(Just a pointer being passed)

    As my called function changes the array for its purpose the original array from the calling function is being changed.

    Is there any way round this without copying the passed array in the called function to a temporary array (its rather large!)



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    One way or another, you'll end up copying the array - either you copy it, or you make the compiler copy it for you (not easy)

    Besides, it's not hard - its just a call to memcpy and you're done

  3. #3
    You're passing a Pointer to a Memory location.
    The Subroutine/Funtion is changing the values in these Memory locations, so yes, the original Array is being changed.

    It depends on what you are trying to do as to what possible solutions there are. The most likely is that you will have to duplicate your Array.

  4. #4
    Hi again,

    would a memcpy() operation involve using malloc or calloc (slow) to free up the memory?

    cheers again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM