C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2009, 06:56 AM   #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
txcs is offline   Reply With Quote
Old 04-22-2009, 07:11 AM   #2
Making mistakes
 
Join Date: Dec 2008
Posts: 347
in function calls, arrays are promoted to pointers. So char array[] == char *array.
Brafil is offline   Reply With Quote
Old 04-22-2009, 07:18 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-22-2009, 10:17 AM   #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
txcs is offline   Reply With Quote
Old 04-22-2009, 10:36 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Reply

Tags
arrays, functions, pointers

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using values from other functions and parameter passing help pokiepo C Programming 2 07-03-2008 11:31 AM
Passing pointers between functions heygirls_uk C Programming 5 01-09-2004 06:58 PM
Functions returning char arrays turmoil C Programming 3 05-27-2003 01:43 AM
passing arrays through functions Ecko C++ Programming 4 04-08-2003 08:21 PM
Passing multidimensional arrays to functions maxthecat C Programming 3 12-22-2001 03:58 PM


All times are GMT -6. The time now is 11:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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