Like Tree1Likes
  • 1 Post By manasij7479

What's wrong with this array declaration?

This is a discussion on What's wrong with this array declaration? within the C Programming forums, part of the General Programming Boards category; Hi, I got an assignment of doing a program to make the sum, substraction, etc. of a matrix A and ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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.)
    stahta01 likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 11:23 PM
  3. array declaration
    By ni00 in forum C Programming
    Replies: 6
    Last Post: 07-06-2009, 07:34 AM
  4. Wrong declaration, Right result ?
    By The SharK in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2005, 02:11 PM
  5. Anything wrong with this declaration?
    By Flick in forum C Programming
    Replies: 4
    Last Post: 08-21-2003, 07:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21