passing arrays to functions

This is a discussion on passing arrays to functions within the C Programming forums, part of the General Programming Boards category; Hi, I'm having trouble passing an array to a function. Arrays are passed "call by value" which means the array ...

  1. #1
    Unregistered
    Guest

    Question passing arrays to functions

    Hi,

    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!)

    Cheers

    Jim

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    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
    Unregistered
    Guest
    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
    Unregistered
    Guest
    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, 05: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, 02:58 PM

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