Thread: What's wrong with this array declaration?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    11

    What's wrong with this array declaration?

    Hi, I got an assignment of doing a program to make the sum, substraction, etc. of a matrix A and a matrix B of given dimensions (Via keyboard, via detection, any method was possible).

    I used dynamic memory allocation with three double ** A, **B, **C and used calloc to create the 2 dim arrays but, then I found this code on the net:

    Code:
    #include<stdio.h>
    
    int main(){
    
       int N,M,L;
    
       printf("Number of rows in Matrix A: ");
       scanf("%d",&N);
       printf("Number of columns in Matrix A: ");
       scanf("%d",&M);
       printf("Number of columns in Matrix B: ");
       scanf("%d",&L);
    
       double A[N][M],B[M][L],C[N][L];
    
      // REST OF CODE
      return 0;
    }
    No warning, compiles, and is shorter than memory allocation, as far as I know, array dimensions must be const int but here they are read from the stdin with a plain scanf and it surprisingly works. Is there anything wrong with doing this?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    It is alright, nowadays. ( Variable Length - Using the GNU Compiler Collection (GCC) )
    But if your matrices are going to be passed around a lot, you're better off using dynamic allocation.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    I see, this is pretty new to me.
    My lecturer is always angry at us when we forget to make dimensions constant, I might use this to trim simple programs then, thanks for the reply

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by dxgalaxy View Post
    I see, this is pretty new to me.
    My lecturer is always angry at us when we forget to make dimensions constant, I might use this to trim simple programs then, thanks for the reply
    Beware that this code may not be accepted by your teacher.
    Education standards are generally *much* back dated that than the language standards.

    Use dynamic allocation as you'd be expected to. (And will need to, in any serious C program.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array declaration
    By scifreak in forum C Programming
    Replies: 4
    Last Post: 12-18-2010, 10:43 AM
  2. Array Declaration
    By zachsformacs95 in forum C Programming
    Replies: 10
    Last Post: 04-20-2010, 10:23 PM
  3. array declaration
    By ni00 in forum C Programming
    Replies: 6
    Last Post: 07-06-2009, 06:34 AM
  4. Wrong declaration, Right result ?
    By The SharK in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2005, 01:11 PM
  5. Anything wrong with this declaration?
    By Flick in forum C Programming
    Replies: 4
    Last Post: 08-21-2003, 06:53 PM