Thread: array passing

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    array passing

    What i'm wondering is if there is an easy way to pass a copy of an array to a function. in the following program i pass the address of an array of numbers, a, to the change function. But what i really want to do is pass a copy so that the function change can not change a. How can i do this?

    Code:
    #include <iostream>
    
    void swap (int &a, int &b) {
    int temp;
    temp=a;
    a=b;
    b=temp;
    }
    
    void display (int numbers[5]) {
    for (int i=0; i <5; i++)
    std:: cout << numbers[i] << ",";
    std::cout << "\n";
    
    }     
    void change (int numbers[5]) {
      numbers[0]=100;numbers[1]=100;numbers[2]=100;
      display(numbers);
    
    }
    
    
    int main(void) {
    int a[]={1,5,6,8,3};
    std::cout << "before organize and print a="; display(a);
    change(a);
    std::cout << "after a="; display(a);
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Uhm some suggestions

    Well maybe you can add a const to your function head so that it won't change ur parameters
    else try to copy the whole array to either another array or maybe even a dynamic array so u can delete the array later..
    Use:
    something like this:

    typeOfYourPointer *ptrName;

    ptrName = new YourArrayType [];

    to create a dynamic array
    and delete it later like this

    delete prtName;

    hope i help a bit ;P

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Stuff allocated with new[] MUST be freed with delete[]!!!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: array passing

    Originally posted by manwhoonlyeats
    What i'm wondering is if there is an easy way to pass a copy of an array to a function. in the following program i pass the address of an array of numbers, a, to the change function. But what i really want to do is pass a copy so that the function change can not change a. How can i do this?
    The easiest way is to encapsulate your array int a container and have your function take the container by value (and you can template the function to make it use that definition for all container types). I'd suggest using boost::array< int, 5 > (boost.org) if you are sure that you always need an array of 5 elements, or you can use std::vector (but I wouldn't recommend it as it forces dynamic memory allocation unless you wanna have some fun with allocators).
    Another quick solution would be to encapsulate your array in a struct and let the default copy constructor do the work (but I don't recommend it).
    Finally, you could always just manually copy all of the elements from within the functions, but I don't recommend that at all.

    Also, just as a side note, your current functions are set up to accept pointers to ints. If you want the functions to only take arrays of ints of length 5 (passed by reference), then use

    void change( const int (&numbers)[5] ) // a reference to an array of 5 const ints

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The const keyword is what you need to prevent modification from responsible programmers.

    There's nothing you can do to prevent irresponsible programmers from messing your stuff up.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM