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 a 2d array as a parameter 1

Status
Not open for further replies.

aaadetos

Programmer
Nov 21, 2004
54
US
Hi..
How would one pass a 2d array as a parameter? Say we have a function:
Code:
void whatever(double *age, int Size)//**
{
for (i=0;i<Size;i++)
{
for (j=0;j<Size;j++)
...
blah,blah,blah
age[i][j]=pow(blah,2);//ERRor c2109 : a subsript was used on a variable that was not an array
}
how would the parameter age on line ** be specified in the function header to prevent this error?
 
Code:
void whatever([COLOR=blue]double age[10][10][/color], int Size)
{
  for (j=0;j<Size;j++) /* etc */
}

int main ( ) {
  [COLOR=blue]double age[10][10][/color];
  double years[20][10];
  whatever( age, 10 );
  whatever( years, 20 ); /* minor dimensions match */
  return 0;
}
Notice that the array parameter in the function is identical to the declaration of the array you want to pass to the function.
You can pass any array which matches the type and all the minor dimensions, to the same function.


You could also write the function with these signatures as well, and it would be the same thing, but they take more effort than simple copy/paste.
Code:
void whatever(double age[][10], int Size)
void whatever(double (*age)[10], int Size)

--
 
Thank you.
Is
Code:
void whatever(double (*age)[10], int Size)
the same as:
Code:
void whatever(double *age[10], int Size)
 
No it isn't - the () matter
First one is a pointer to an array[10] of double
Second one is an array[10] of pointers to double

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top