Thread: function formal parameter pointer style for arrays

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    function formal parameter pointer style for arrays

    Which style is better when declaring formal parameters in functions concerning pointers and arrays.

    int sum(int * array, int n) or
    int sum(int array[], int n).

    I think the first is better because it leaves out any ambiguity that may occure if someone does not know that in this context int arrray[] is acctually a pointer to int.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Personally, I try to use std::vector in the place of arrays, which is not only completely unambiguous, but you can do nice things like determine the array size within the function.

    I tend to use pointer notation if I'm forced to use one of the two.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I only ever use vectors when I don't know the number of elements that will be in the array or the array is going to be/can be quite large. For example, an array of 5 ints, I wouldn't bother, but for an array of 50 class structures, I would use a vector.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I tend to use vectors in 3 cases:

    1) I don't know the size at compile time. Directly using dynamic memory allocation using new[]/delete[] is much more prone to errors and memory leaks, and I know myself well enough to know I'm not immune to dumb mistakes.

    2) I plan on passing the array to a function that will need to know its size. I find it easier to use a vector and let the function discover the size rather than using a const variable for the size.

    3) I plan to use multidimentional arrays.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I haven't learned about vectors yet but arrays in which you pass the size may have an advantage when for instance you pass only part of the array say the first 3 elements or when you pass elements starting say with the 3rd element to the end of the array.

    ie

    int total;
    int arr[10];

    // pass only first 3 elements
    total=sum(arr,3);

    // pass 3 elements starting with 3th index on
    total=sum(arr+3,3);

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can do the same with std::vectors. You just have to use iterators instead of simply pointers.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. function pointer & Thread Parameter :: Multithreading
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 09-11-2002, 08:42 AM