See "LinuxC Programming Practice" (see the http://www.linuxidc.com/Linux/2011-01/31315.htm) on the 67 pages said intn = 10; inta [n]; that is not correct, And then I tested it, I found it was correct. I found the dynamic array allocation on the Internet. Most people say yes.
See "Linux C Programming Practice" (see http://www.linuxidc.com/Linux/2011-01/31315.htm) on the 67 pages said
- IntA [n];
I checked it and found it was correct. I found the dynamic array allocation on the Internet. Most people say they should use pointers or malloc for allocation.
I am writing this article to tell you that what is mentioned in the book is not necessarily correct. The following example is provided to test the problem.
- # Include
- IntMain ()
- {
- IntCol, row; // indicates the row and column
- Printf ("Enter the number of rows :");
- Scanf ("% d", & col );
- Printf ("Enter the number of columns :");
- Scanf ("% d", & row );
- IntA [col] [row]; // facts prove that this is usable, as long as there is a value in the Declaration.
- IntI, j;
- For(I = 0; I <col; I ++)
- {
- For(J = 0; j <row; j ++)
- {
- Printf ("Enter the number of % d columns in row % d:", (I + 1), (j + 1 ));
- Scanf ("% d", & a [I] [j]);
- }
- }
- Printf ("array: \ n ");
- For(I = 0; I <col; I ++)
- {
- For(J = 0; j <row; j ++)
- {
- Printf ("\ t % d", a [I] [j]);
- }
- Printf ("\ n ");
- }
- Return0;
- }
The running result is as follows:
- Mars @ Ubuntu :~ /Desktop/C $ gcc demo4_7.c-o demo4_7
- Mars @ ubuntu :~ /Desktop/C $./demo4_7
- Enter the number of rows: 3
- Enter the number of columns: 2
- Enter the number of 1 columns in 1st rows: 1
- Enter the number of 2 columns in the 1st rows: 2
- Enter the number of 1 columns in 2nd rows: 3
- Enter the number of 2 columns in the 2nd rows: 4
- Enter the number of rows and 1 column in 3rd: 5
- Please input 3rd rows and 2 columns: 6
- Array:
- 1 2
- 3 4
- 5 6