Dynamic Allocation of C language arrays in Ubuntu

Source: Internet
Author: User
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

  • IntN = 10;
  1. 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.

  1. # Include
  2. IntMain ()
  3. {
  4. IntCol, row; // indicates the row and column
  5. Printf ("Enter the number of rows :");
  6. Scanf ("% d", & col );
  7. Printf ("Enter the number of columns :");
  8. Scanf ("% d", & row );
  9. IntA [col] [row]; // facts prove that this is usable, as long as there is a value in the Declaration.
  10. IntI, j;
  11. For(I = 0; I <col; I ++)
  12. {
  13. For(J = 0; j <row; j ++)
  14. {
  15. Printf ("Enter the number of % d columns in row % d:", (I + 1), (j + 1 ));
  16. Scanf ("% d", & a [I] [j]);
  17. }
  18. }
  19. Printf ("array: \ n ");
  20. For(I = 0; I <col; I ++)
  21. {
  22. For(J = 0; j <row; j ++)
  23. {
  24. Printf ("\ t % d", a [I] [j]);
  25. }
  26. Printf ("\ n ");
  27. }
  28. Return0;
  29. }

The running result is as follows:

  1. Mars @ Ubuntu :~ /Desktop/C $ gcc demo4_7.c-o demo4_7
  2. Mars @ ubuntu :~ /Desktop/C $./demo4_7
  3. Enter the number of rows: 3
  4. Enter the number of columns: 2
  5. Enter the number of 1 columns in 1st rows: 1
  6. Enter the number of 2 columns in the 1st rows: 2
  7. Enter the number of 1 columns in 2nd rows: 3
  8. Enter the number of 2 columns in the 2nd rows: 4
  9. Enter the number of rows and 1 column in 3rd: 5
  10. Please input 3rd rows and 2 columns: 6
  11. Array:
  12. 1 2
  13. 3 4
  14. 5 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.