Parse error in one compiler but not in another...

This is a discussion on Parse error in one compiler but not in another... within the C Programming forums, part of the General Programming Boards category; Hey, I just noticed that (with Dev-C++ 4.01, Win98se) when I try to declare a new variable or array or ...

  1. #1
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    Question Parse error in one compiler but not in another...

    Hey,

    I just noticed that (with Dev-C++ 4.01, Win98se) when I try to declare a new variable or array or whatnot after doing anything other than declaring the first variables, like in the following example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/* Main variables */
    	/* m = base; n = digits */
    	int m, n;
    	
    	/* Counter variables */	
    	int i, j, k;
    	
    	/* Accept values of m & n only if within limits specified */
    	do
    	{
    		printf("\nEnter m (0 < m < 11): ");
    		scanf("%d", &m);
    	}
    	while(m < 1 || m > 10);
    	
    	do
    	{
    		printf("\nEnter n (0 < n < 81): ");
    		scanf("%d", &n);
    	}
    	while(n < 1 || n > 80);
    	
    	/* Catch the wretched '\n' scanf() is so fond of leaving behind */
    	getchar();
    	/* Plainly esthetic '\n' */
    	putchar('\n');
    	
    	/* Array to hold the numbers */
    	int nsys[n];
    
                    /* ... */
    I always get a parse error on the line that declares the new var or array.

    However, I don't get this error when I try to compile the same code in 'JFE and GCC' (a small old IDE i can't remember where I found)...

    I tried the Dev-C++ mailing list but haven't gotten any useful replies, so I thought I'd try here.

    As far as I know there's nothing inherently wrong with that code, or is there?

    Well, thanks in advance for any help in clearing this up...

    adios,
    biterman.

    -----------------------
    Oh, I accidentally posted this in the C++ board. I apologize. Should I repost it in the C board?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I seem to remember reading somewhere that variables in C have to be declared at the beginning of the program whereas in C++ they can be declared anywhere throughout the program, as long as they are declared before they are used. Most compilers these days aren't strictly C, but will do both C and C++. So if you write a program using C syntax but compile it with a C++ compiler, you can probably declare variables using C++ rules. But if you use a C compiler, then you need to follow C rules regarding location for variable declaration. Even in C++ some people prefer to declare all variables at the beginning of the program, but the other option is available. Unfortunately, I don't have the reference for this theory available, but someone will correct me if I'm wrong.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    so, apparently, using Dev C++, if you use stdio as the input/output routine (which is C style input/output) without iostream (which is the C++ style input/output routine) then the compiler enforces C style variable declaration. If you add iostream.h to the list of includes and leave stdio there as well, it may or may not allow C++ style variable declaration. If you use just iostream, without stdio, it should allow for the variable declaration style you indicate, but you won't be able to use printf(), etc.

  4. #4
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98
    Thanks for clearing that up

    So if I can't declare the array in the middle of the program, how can I make it the right size? The whole program depends on the size of that array and the size of the array depends on user input. How can I make it work using plain C?

    Memory allocation? I haven't learned how to do that yet...

    adios,
    biterman.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int n;
    char array[n];

    will fail irrespective of whether compiled under C or C++. The reason is that an array size must be available at compile time for static arrays. That is n must be constant.

    const int n = 10;
    char array[n];

    works fine

    char array[10];

    works fine.

    But, if you don't know the size of the array you want to use at compile time then you need to use dynamic memory with memory allocation routines for the language you are using--malloc/free in C or new/delete in C++ (or appropriate alternatives where necessary---like new/delete [] for arrays in C++ or calloc/free in C or whatever).

  6. #6
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98
    Ok, I understand your point now. And I just confirmed it for myself, if I change the file type to .cpp it'll compile and run smoothly with Dev-C++.

    Guess I'll go learn how to dinamically allocate memory

    thanks again.

    adios,
    biterman.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 05:42 AM
  2. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM
  5. Compiler questions
    By DvdHeijden in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 02:00 PM

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