fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(void) {
  6. int ( *array )[10] = malloc( sizeof(int ) * 20 * 10 );
  7. for( size_t i = 0 ; i < 20 ; i++ )
  8. for( size_t j = 0 ; j < 10 ; j++ )
  9. array[i][j] = i * 100 + j;
  10.  
  11.  
  12.  
  13. for( size_t i = 0 ; i < 20 ; i++ )
  14. {
  15. for( size_t j = 0 ; j < 10 ; j++ )
  16. {
  17. printf( "%5d " , array[i][j] );
  18. }
  19. printf( "\n" );
  20. }
  21.  
  22. array = realloc( array , sizeof( int ) * 15 * 10 );
  23.  
  24. printf( "\n\n" );
  25. for( size_t i = 0 ; i < 15 ; i++ )
  26. {
  27. for( size_t j = 0 ; j < 10 ; j++ )
  28. {
  29. printf( "%5d " , array[i][j] );
  30. }
  31. printf( "\n" );
  32. }
  33.  
  34. int (*newarray)[3] = ( int(*)[3] )array;
  35. for( size_t j = 1 ; j < 15 ; j++ )
  36. {
  37. memmove( newarray[j] , array[j] , sizeof( int ) * 3 );
  38. }
  39.  
  40. newarray = realloc( array , sizeof( int ) * 15 * 3 );
  41.  
  42.  
  43. printf( "\n\n" );
  44. for( size_t i = 0 ; i < 15 ; i++ )
  45. {
  46. for( size_t j = 0 ; j < 3 ; j++ )
  47. {
  48. printf( "%5d " , newarray[i][j] );
  49. }
  50. printf( "\n" );
  51. }
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 2288KB
stdin
Standard input is empty
stdout
    0     1     2     3     4     5     6     7     8     9 
  100   101   102   103   104   105   106   107   108   109 
  200   201   202   203   204   205   206   207   208   209 
  300   301   302   303   304   305   306   307   308   309 
  400   401   402   403   404   405   406   407   408   409 
  500   501   502   503   504   505   506   507   508   509 
  600   601   602   603   604   605   606   607   608   609 
  700   701   702   703   704   705   706   707   708   709 
  800   801   802   803   804   805   806   807   808   809 
  900   901   902   903   904   905   906   907   908   909 
 1000  1001  1002  1003  1004  1005  1006  1007  1008  1009 
 1100  1101  1102  1103  1104  1105  1106  1107  1108  1109 
 1200  1201  1202  1203  1204  1205  1206  1207  1208  1209 
 1300  1301  1302  1303  1304  1305  1306  1307  1308  1309 
 1400  1401  1402  1403  1404  1405  1406  1407  1408  1409 
 1500  1501  1502  1503  1504  1505  1506  1507  1508  1509 
 1600  1601  1602  1603  1604  1605  1606  1607  1608  1609 
 1700  1701  1702  1703  1704  1705  1706  1707  1708  1709 
 1800  1801  1802  1803  1804  1805  1806  1807  1808  1809 
 1900  1901  1902  1903  1904  1905  1906  1907  1908  1909 


    0     1     2     3     4     5     6     7     8     9 
  100   101   102   103   104   105   106   107   108   109 
  200   201   202   203   204   205   206   207   208   209 
  300   301   302   303   304   305   306   307   308   309 
  400   401   402   403   404   405   406   407   408   409 
  500   501   502   503   504   505   506   507   508   509 
  600   601   602   603   604   605   606   607   608   609 
  700   701   702   703   704   705   706   707   708   709 
  800   801   802   803   804   805   806   807   808   809 
  900   901   902   903   904   905   906   907   908   909 
 1000  1001  1002  1003  1004  1005  1006  1007  1008  1009 
 1100  1101  1102  1103  1104  1105  1106  1107  1108  1109 
 1200  1201  1202  1203  1204  1205  1206  1207  1208  1209 
 1300  1301  1302  1303  1304  1305  1306  1307  1308  1309 
 1400  1401  1402  1403  1404  1405  1406  1407  1408  1409 


    0     1     2 
  100   101   102 
  200   201   202 
  300   301   302 
  400   401   402 
  500   501   502 
  600   601   602 
  700   701   702 
  800   801   802 
  900   901   902 
 1000  1001  1002 
 1100  1101  1102 
 1200  1201  1202 
 1300  1301  1302 
 1400  1401  1402