Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an array as a function argument.Help needed.

Status
Not open for further replies.

DazedNConfused

Programmer
Mar 18, 2002
8
US
In book it says that arrays cannot be passed by value to parameters of a function,but then in example it passes an array to an array parameter .I understand if you pass adress of an array to a pointer parameter,but how can you pass an adress to an array or do you pass something else?And to confuse me even more,when he explains why square brackets are empty,he says "THE SIZE OF THE FIRST DIMENSION OF AN ARRAY IS NOT PART OF ITS TIPE."Can someone please explain to me what he meant?

double x(double array[]) //header of a function




I thank you
 
HEllo?Isn't there anyone who could answer me this.If you don't understand the question tell me so.But pleeeeaseee answer this tiny thing for yours truly.Thank you.
 
You only pass a referense to an array, say you have an array
double arr[3];
when you call you function use
my_func(arr)
// jocke
 
Following function definitions are identical:

void x(double array[]);
void x(double *array);

Both formal parameters are of type pointer to double and actual parameter array elements can be accessed as array or *(array+i) in both cases.
Calls to such functions also can have two identical forms:

double data[10];

x(data);
x(&data[0]);

In both cases address of the very first element of an array will be passed.

 
Sorry, the right text:

Following function definitions are identical:

void x(double array[]);
void x(double *array);

Both formal parameters are of type pointer to double and actual parameter array elements can be accessed as array or *(array+i) in both cases.
Calls to such functions also can have two identical forms:

double data[10];

x(data);
x(&data[0]);

In both cases address of the very first element of an array will be passed.
 
As far as I understand pointers and such,

double array[]

is the same as

double * array

except for the convenience of reminding you you have an array as an argument, not just a pointer to a single "double" variable. This is why it is passed as reference.

"THE SIZE OF THE FIRST DIMENSION OF AN ARRAY IS NOT PART OF ITS TIPE.":

If you want a 2-dimensional array, you would have either

double x(double array[][10]) //header of a function

or

double x(double *array[10]) //header of a function

In both case a "pointer to an array of 10 doubles", and this pointer could be the adress of an array.

E.g.:

double d[10][10]

x( d );

or

x( &d[0][0] );

or

x( d[0] );

should all give the same (might want to test this).

Vincent
 
Vincent, i do not agree:

"double *array[10]" as a formal argument of the function is a pointer to a single-dimensional array of 10 pointers (expression array[2][3] does mean (array[2])[3], where element array[2] is a pointer to single-dimensional array of 10 doubles), where

"double array[][10]" is simply pointer to double, which can be accessed through double dimensional indexes.

Multidimensional arrays in C(++) are allocated sequentially in memory, rows by rows, so element array[2][3] can be accessed as ((double *)array)[2*10+3]

 
Mingis,

You are right. What I meant was "double (*array)[10]", which IS equivalent to "double array[][10]", but I don't think anyone would write (*array)[10], that does not look simple...

Pointers are not easy :)

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top