Passing an array to a function, preferred way?

This is a discussion on Passing an array to a function, preferred way? within the C++ Programming forums, part of the General Programming Boards category; Is there a preferred or standard method for passing an array to a function as a parameter? First day of ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Passing an array to a function, preferred way?

    Is there a preferred or standard method for passing an array to a function as a parameter? First day of Advanced C++ today at college and we're working on some homework and the teacher wasn't much help. Our segment was to pass an array of integers to a function, and average them. Setting up the function I noticed that these both work:


    Code:
    int avgNums(int* array, int n)     // Pass pointer to array address
    int avgNums(int array[], int n)     // Pass array address
    I understand that they both point to the address for the array, but was wondering if there's a preferred or standard way to do this. I know there's something similar to how you define things as NULL in the new c++11, so was just looking for the right way to learn.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    3,268
    For a single dimensioned array either method is correct. With both you're passing the array address, not a pointer to an array address.

    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    They both are fine if you are working with built-in arrays. The idea is that the pointer parameter will point to the first element of the array argument. However, I would declare the pointer to be a pointer to const int since the function would not be changing the elements of the array.

    An alternative:
    Code:
    int avgNums(const int* begin, const int* end)
    For this version, begin will point to the first element of the array, end will point to one past the last element of the array. It then becomes possible for you to take the average of a range, e.g., the second half of the array. (Well, it is possible with the pointer + offset version too, except that it would be somewhat less typical.)
    Last edited by laserlight; 09-24-2012 at 08:24 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 12:32 PM
  2. Passing array into function
    By kulfon in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 10:08 PM
  3. Passing an Array to a function
    By mmx4000 in forum C Programming
    Replies: 2
    Last Post: 06-19-2007, 01:29 PM
  4. Passing 2d Array to function?
    By Marcosm64 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2003, 02:02 PM
  5. 2D array passing to function
    By mkorolen in forum C Programming
    Replies: 5
    Last Post: 03-31-2002, 07:37 PM

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