Passing array as argument?

This is a discussion on Passing array as argument? within the C++ Programming forums, part of the General Programming Boards category; Hi, Just out of curiosity, how do I pass an array as an argument by value instead of by reference? ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Question Passing array as argument?

    Hi,

    Just out of curiosity, how do I pass an array as an argument by value instead of by reference?

    Is the only way to send the pointer of the array and then copy it manually?

    Thanks in advance.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can't, unless you pass each element of the array separately.
    zen

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Increment pointers

    Pass a pointer to the first element of the array, and then increment the pointer with out doing any dereferencing, increment it.

    Sean Mackrory
    sean_mackrory@hotmail.com

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Declaration
    int *array=new int[20];

    Prototype
    void UseArray(int *passedarray);


    Calling
    UseArray(array);

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Both of the above two examples will pass the array by reference, not by value (any modification to the array in the function will be made to the original). Whenever an address of a variable is passed into a function it is passed by reference, and to pass an array you need to pass it's address.
    zen

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    int array[10];

    void UseArray(int &array)
    {
    int *ptr=&Array;
    for (int i=0;i<endofarray;i++,ptr++)
    {
    printf("%d\n",*ptr);
    }
    }

    UseArray(*array);


    Don't use this way very often.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    put the array in a struct and pass the struct by value.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    Put the keyword "const" in front of the array ''
    -----------------------------------------------
    everready

    To code, or not to code, that is the question.

    Well the answer is 'TO CODE' of cause

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 06:23 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM

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