可以用二維數(shù)組名作為實參或者形參,在被調用函數(shù)中對形參數(shù)組定義時可以指定所有維數(shù)的大小,也可以省略第一維的大小說明,如:
void Func(int array[3][10]);
void Func(int array[][10]);
例2 將二維數(shù)組依舊當作二維數(shù)組來處理
下面是一個字符串數(shù)組的參數(shù)傳遞程序,實現(xiàn)將字符串數(shù)組中的字符串按照從小到大的順序進行排序:
//WordSort實現(xiàn)了對5個字符串的排序
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void WordSort(char p[][10],int RowSize)
{
int n=0,m;
char temp[10];
for(n=0;n
for(m=n+1;m
if( strcmp(p[m],p[n]) < 0 )
{
strcpy(temp,p[n]);
strcpy(p[n],p[m]);
strcpy(p[m],temp);
}
for(n=0;n<5;++n)
printf("In subfunction:%s\n",p[n]);
}
void main()
{
int k=0;
char word[5][10];
for(;k<5;++k)
scanf("%s",&word[k]);
WordSort(word,5);
printf("sorted word:\n");
for(k=0;k<5;k++)
printf("In main function:%s\n",word[k]);
system("pause");
}
例3 用“行指針”傳遞參數(shù)
運行下面程序:
/************************************/
//二維數(shù)組作為形參的參數(shù)傳遞方式之一
/************************************/
#include
void print_array_1(int (*a)[3], int Row_Size)
{
int j;
for(j=0;j<3*Row_Size;j++)
{
printf("%d ",(*a)[j]);
if(j%3==2) printf("\n");
}
}
void print_array_2(int (*a)[3], int Row_Size)
{
int i,j;
for(i=0;i
{
for(j=0;j<3;j++)
printf("%d ",*(*(a+i)+j));
printf("\n");
}
}
void main()
{
int i,j,value=0;
int a[4][3]={0};
for(i=0;i<4;i++)
for(j=0;j<3;j++)
a[i][j]=value++;
print_array_1(a,4);
printf("\n");
print_array_2(a,4);
}