(2)连续的方法:
- int **matric ;
- matric = (int**)malloc(M*sizeof(int*)) ;
- matric[0] = (int*)malloc(sizeof(int)*M*N) ;
- for(i = 1 ; i<M ; i++)
- {
- matric[i] = matric[i-1] + N ;
- }
- for(i = 0 ; i<M ; i++)
- for(j=0 ; j<N ; j++)
- {
- printf("Please entry the %d value:\n",i*N+j);
- scanf("%d",(*(matric+i)+j));
- }
- for(i = 0 ; i<M ; i++)
- {
- switch(i)
- {
- case 0 :
- printf("第一行数组元素:\n");
- break ;
- case 1:
- printf("第二行数组元素:\n") ;
- break ;
- case 2:
- printf("第三行数组元素:\n") ;
- break;
- }
- for(j = 0 ; j<N ; j++)
- {
- printf("The values of %d elements are:%d\n",i*N+j,*(*(matric+i)+j));
- printf("The pointer position is:%x\n",(*(matric+i)+j)) ;
- }
- }
运行结果为:

可见,内存都是连续的。
