fork(4) download
  1. #include<stdio.h>
  2.  
  3.  
  4. int c_array(int **a,int row,int column,int i,int j)
  5. {
  6. printf("%d ",*(*( a + i) +j) );//使用pointer來控制矩陣
  7. }
  8.  
  9.  
  10. int main(void)
  11. {
  12. int **ptr=NULL;
  13. int row,column;
  14. int i,j;
  15.  
  16. //while(1)
  17. {
  18.  
  19. row=5;
  20. column=5;
  21.  
  22. ptr=(int**)malloc(sizeof(int*)*row);
  23. //生成一維指標陣列
  24. for(i=0;i<row;i++)
  25. {
  26. ptr[i]=(int*)malloc(sizeof(int)*column);
  27. }//二維
  28.  
  29. for(i=0;i<row;i++)
  30. {
  31. for(j=0;j<column;j++)
  32. {
  33. ptr[i][j]=1;
  34. }
  35. }//將矩陣付值:1
  36.  
  37. for(i=0;i<row;i++)
  38. {
  39. for(j=0;j<column;j++)
  40. {
  41. //printf("%d",ptr[i][j]);//測試用
  42. c_array((int*)ptr,row,column,i,j);//使用另外函數來print
  43. }
  44.  
  45. printf("\n");//換行
  46. }
  47.  
  48. for(i=0;i<row;i++) free(ptr[i]);
  49. free(ptr);//釋放記憶體
  50.  
  51. }
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 2140KB
stdin
Standard input is empty
stdout
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1