creating an array of input size..

This is a discussion on creating an array of input size.. within the C Programming forums, part of the General Programming Boards category; when i use the input to set the size of the array i get cd.c(8) : error C2143: syntax error ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    creating an array of input size..

    when i use the input to set the size of the array
    i get
    cd.c(8) : error C2143: syntax error : missing ';' before 'type'
    cd.c(9) : error C2143: syntax error : missing ';' before 'type'
    cd.c(10) : error C2143: syntax error : missing ';' before 'type'
    Code:
    #include <stdio.h>
    int main(){//star
    	int rows,cols,jndex,input;
    	int sum2=0;
    	 
    	scanf("%d %d",&rows,&cols);
    	
    	int index,kndex,tndex,gndex,lndex;  //line 8
       int rows_sum[rows];                                     //line9
       int matrix[rows][cols];                                   //line10
    
    	return 0;
    }//end main func

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's a "feature" of C99 (I think) -- if not then I know it's a compiler extension.

    Use the heap (ie malloc() / free()) instead.

    For example:
    Code:
    int rows_sum[rows];
    Can be written as:
    Code:
    int * rows_sum = NULL;
    
    rows_sum = malloc(rows * sizeof(*rows_sum));
    
    /* ... do stuff with it */
    
    free(rows_sum);
    rows_sum = NULL;

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this code works in codeblock 8.02
    why its not working on vs2005

    i cant use
    malloc() / free()



    is there any simpler way
    ??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right, because gcc uses the 1999 standard and Visual Studio uses the 1989 standard. In the 1989 version, there is no way to do this other than using malloc. (Well, you can arbitrarily declare int rows_sum[1000] and hope that the user doesn't want more than 1000 rows. If you have a maximum matrix size, then you use it here.)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by transgalactic2
    i cant use
    malloc() / free()
    So, you cannot use malloc() and free(), and you cannot use variable length arrays. What exactly can you use? Anything else that we suggest will be platform dependent... or you have to allocate on the stack as much memory as you will ever need, and then do your own memory management.
    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

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this stuff works on codeblocks

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    codeblocks isn't a compiler. As tabstop mentions gcc implements (most of) the C99 standard which defines runtime-allocated static arrays like this. You're most likely using codeblocks with gcc (perhaps MinGW).

    Perhaps if you explain why you can't use malloc()/free()? Or better yet, what problem are you trying to solve?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since your instructor (apparently) has declared that 50 is the largest size of your matrix (per the other threads on this problem), then use 50 as the size of your arrays and be done with it.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by tabstop View Post
    Since your instructor (apparently) has declared that 50 is the largest size of your matrix (per the other threads on this problem), then use 50 as the size of your arrays and be done with it.
    but if i will need to transpose a matrix

    it will print a 50 X 50 array

    how can i print a small matrix
    ??

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Define some "virtual" boundaries.

    ie;
    Code:
    #define MAX_ROWS 50
    #define MAX_COLS 50
    
    /* ... */
    int matrix[MAX_ROWS][MAX_COLS] = {0};
    int   numRows = 0,
          numCols = 0;
    
    /* read the matrix in setting numRows & numCols accordingly with a max of MAX_ROWS and MAX_COLS respectively */
    
    /* use numRows and numCols to print the matrix (instead of MAX_ROWS & MAX_COLS) */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM

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