Thread: Passing array as argument?

  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
    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
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    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
    [email protected]

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

    Prototype
    void UseArray(int *passedarray);


    Calling
    UseArray(array);

  5. #5
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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, 05:23 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  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, 07:15 AM