m和n不确定,所以要在输入m和n后动态创建二维数组。举例代码如下:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int main(void){
int m,n,i,j;
char **p;
printf("Input m & n(>0 int)...\n");
while(scanf("%d%d",&m,&n),m<=0 || n<=0)
printf("Error, redo:\n");
if((p=(char **)malloc(sizeof(char *)*m))==NULL){
printf("Application memory failure...\n");
exit(0);
}
for(i=0;i<m;i++)
if((p[i]=(char *)malloc(sizeof(char)*n))==NULL){
printf("Failed to create the array...\n");
exit(0);
}
srand((unsigned)time(NULL));
for(i=0;i<m;i++){//给二维数组赋随机01值并顺便打印出来
for(j=0;j<n;printf("%2d",p[i][j++]=rand()%2));
printf("\n");
}
for(i=0;i<m;free(p[i++]));
free(p);
return 0;
}