Problem with definitions and executable statements

This is a discussion on Problem with definitions and executable statements within the C Programming forums, part of the General Programming Boards category; I wrote a program to calculate the tranpose of a matrix. The declaration part of my code is Code: int ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17

    Problem with definitions and executable statements

    I wrote a program to calculate the tranpose of a matrix. The declaration part of my code is
    Code:
    int m, n; //m is the number of rows of the array and n is the number of columns of the array
    int i,j;
    printf("What is the order of the matrix. Input number of ROWS and COLUMNS: ");
    scanf("%d%d",&m,&n);
    int number[m][n];
    int transpose[n][m];
    I didnt want to use fixed integers in the arrays because I wanted the user to specify the order of the matrix.
    The program worked well in CodeBlocks 10.05 which is the IDE I use, but when I tried to run it in Turbo C++, the program gave me an error, "Constant Expression required in function main" and the int number[m][n] line was highlighted.
    Can anybody kindly explain to me why the program didnt run in Turbo C++ but worked in CodeBlocks.
    I'm new to C programming, just over a month old. Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,126
    Quote Originally Posted by bluechip
    The program worked well in CodeBlocks 10.05 which is the IDE I use, but when I tried to run it in Turbo C++, the program gave me an error, "Constant Expression required in function main" and the int number[m][n] line was highlighted.
    Can anybody kindly explain to me why the program didnt run in Turbo C++ but worked in CodeBlocks.
    Because on these two lines, you create variable length arrays:
    Code:
    int number[m][n];
    int transpose[n][m];
    The VLA feature was only introduced in the 1999 edition of the C standard (C99), so your older compiler presumably does not provide it.

    By the way, CodeBlocks is an IDE. You are probably using gcc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Because on these two lines, you create variable length arrays:
    Code:
    int number[m][n];
    int transpose[n][m];
    The VLA feature was only introduced in the 1999 edition of the C standard (C99), so your older compiler presumably does not provide it.

    By the way, CodeBlocks is an IDE. You are probably using gcc.
    Thank you very much for your response.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I used Turbo C/C++ ver. 1.01, and what Laserlight said was accurate for that version.

    Have you tried using

    const int variableName;

    yet? That wouldn't work with 1.01 that I had, but would be worth a try with other versions.

    You don't like using r and c variable names for ROWS and COLS?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro definitions problem
    By acpower in forum C Programming
    Replies: 8
    Last Post: 06-15-2012, 02:44 AM
  2. Replies: 23
    Last Post: 03-07-2011, 05:28 PM
  3. Problem with an executable
    By maxorator in forum Tech Board
    Replies: 15
    Last Post: 08-22-2006, 01:53 AM
  4. Replies: 13
    Last Post: 02-20-2005, 12:44 AM
  5. Local funtion definitions are illegal problem
    By Kaashead in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2004, 12:55 PM

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