Thread: Passing Arrays to Functions...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Passing Arrays to Functions...

    Hello All:

    I have a question about how something works with respect to passing an array to a function. First a quick code example:

    Code:
    void cat (int dataA[])
    {
    	//...
    }
    void dog (int dataB[3])
    {
    	//...	 
    }
    
    int main () 
    {
    	int x[10] = {11, 13, 15, 17, 19, 21, 23, 25};
    	cat(x);
    	dog(x);
    	return 0;
    }
    I'm trying to understand some of the details concerning what the int [] as a function parameter really means and how it relates to a pointer.

    1) As a function parameter, what exactly does int dataA[] mean and how does it relate to something like int* dataA? sizeof(dataA) in both cases returns 4. However, if one declares an static local array in a function such as int moo[] = {1, 2, 3}; then sizeof(moo) returns 12. So there must be some difference between int dataA[] and int moo[].

    2)With dataA of function cat (and dataB in function dog), it is possible to increment them as in dataA++; or dataB++; ... This would lead me to think that even though the parameters are declared as something that looks like an array declaration, the compiler actually is just treating them like int*... Am I missing something?

    3)If you do put a number in the [] of a function parameter array, is it used for anything? Does it have any implications? Doesn't seem to matter when sending a 10 element array to dog. Still compiles and executes with no apparent problems. (BTW - i know this shouldn't be done, but i'm still curious of what's going on).

    4) can you suggest some reading material that would specifically address some of these issues? I'm finding a ton of references about "what to do" and "what not to do" (don't put number in [] of function parameters), but not a great deal of "why". Any suggestions would be appreciated.

    Thanks
    -txcs

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    in function calls, arrays are promoted to pointers. So char array[] == char *array.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1) As far as I know, int *a and int a[] are the same thing. If there are exceptions, they are so bizarre/unusual that I can't think of any right now.

    2) See above.

    3) For single dimension arrays, no. It's just there for consistency with multidimension arrays, where the numbers for all dimensions aside from the first one are necessary to allow the function to calculate the index into the actual 1D array that the compiler produces.

    For example:
    Code:
    void func1(int arr[10][15])
    {
    ...
    }
    
    void func2(int arr[][15])
    {
    ...
    }
    
    void func3(int arr[][])
    {
      ... 
    }
    func1 and func2 are both legal and equivalent. func3 is not valid, as the compiler doesn't know the second [] size.


    4). I suppose it just confuses people if you DO put a number in there, and it doesn't match your actual number - if it's a constant declared in one place, I don't really see the problem with supplying a number - but it doesn't serve any purpose, indeed.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Excellent. Thanks for the help.

    One clarification follow-up question, though. It seems from the answers that as far as a function parameter declaration is concerned, there is no difference between void foo(int x[]) and void foo(int*x). Is that reasonable?

    thanks,
    txcs

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by txcs
    One clarification follow-up question, though. It seems from the answers that as far as a function parameter declaration is concerned, there is no difference between void foo(int x[]) and void foo(int*x). Is that reasonable?
    Yes. Note that unless (and sometimes even if) you are using some special value to denote "end of array", you should provide the size of the array as another parameter.

    Also, considering that this is C++, you may want to consider the use of std::vector and std::tr1::array instead, or more generically, an iterator pair to denote a range, depending on your requirements.
    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. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM

Tags for this Thread