for(int x

This is a discussion on for(int x within the C Programming forums, part of the General Programming Boards category; Sorry for such a basic question. I use the following code all the time: Code: for (int x=0;cond;expr) { .... ...

  1. #1
    fou
    Guest

    for(int x

    Sorry for such a basic question. I use the following code all the time:
    Code:
    for (int x=0;cond;expr) {
     ....
    }
    ie. The var x is initialised in the loop starter. A search on this board and comp.lang.c also shows thousands of instances of code like this.
    However, when I do a lint check with this online lint checker:
    http://www.cleanscape.net/products/l...ine/login.html
    It gives me errors such as:
    Code:
    	for (int sectionIndex = 0;sectionIndex < 3;sectionIndex++) {
    	     ^
    mx.c: line 153: Error # 33: Expression expected.
    
    ---------------------------------------------------------------------
    
    	for (int sectionIndex = 0;sectionIndex < 3;sectionIndex++) {
    	         ^
    mx.c: line 153: Warning # 102: Strange use of "for" statement.
    >   The  statement  in question is  of a form which may reflect some
    >   sort of typing error; for example, the statement:
    >   for (i = 0; i < 10; j++) { ... }
    >   should be incrementing "i", not "j".
    
    ---------------------------------------------------------------------
    
    	for (int sectionIndex = 0;sectionIndex < 3;sectionIndex++) {
    	         ^
    mx.c: line 153: Error # 26: ';' expected.
    
    ---------------------------------------------------------------------
    
    	for (int sectionIndex = 0;sectionIndex < 3;sectionIndex++) {
    	         ^
    mx.c: line 153: Error # 21: Undefined symbol: sectionIndex
    Is initialising a var like this not ansi or is this lint checker got problems. If it is not ansi, is there any compilers that do not support it?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    for (int x=0;cond;expr)
    This is C++ code, not C. In C you cannot declare variables in the first step of the for loop. Valid code is:

    int x;
    for( x = 0; cond; expr )


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    fou
    Guest

    Damn and Blast!

    Thanks a lot.

    I understand that it is legal in the c99 standard.? It seems from the quantity of code that contains this syntax that many compilers support it, ansi or not. Mine (lcc-win) does not complain even with the ansic option on.

    Anyway, I better start fixing my code. I also have to remove // coments from my code. If neither of these are legal it would be nice if there wasn't so much sample code/tutorials etc. that contain them.

    Thanks again.

  4. #4
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Hmm... well... at least I won't go making that mistake... heh... I can learn just by watching these threads.

    I've used for (int x=0;x<50;x++) quite a lot, but I guess I'll have to stop.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Frobozz
    Hmm... well... at least I won't go making that mistake... heh... I can learn just by watching these threads.
    Well you'd like to think so anyway. You just have to watch out for the threads where people in insist they're right even when everyone on earth is telling them they're wrong.

    Some people just can't admit they're wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    fou
    Guest
    Thanks all!
    Code:
    for (int p = 4;p;p--) {
      ...
    }
    int p;
    I gather that C99 alllows the initialisation of variables anywhere. However I gather that no special scope is given when initialised in the for starter. So the above would still be illegal. Is this the same in c++ or does p get the scope of the for block in c++?

    Thanks.

  7. #7
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    I also found many problems in this way.
    Even gcc3.2 doesn't support for(int i=...;cond;expr).
    So I use c++ compiler, then.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  8. #8
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    you have to compile with the -std=c99 flag
    I have tested, it works very well with gcc3.2, thanks
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array V2.
    By matsp in forum C++ Programming
    Replies: 38
    Last Post: 11-26-2008, 01:05 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  4. Help With Unhandled Exception
    By MasterOfTheBass in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 01:45 AM

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