
const char *p == char const *p ?
|I am new to C programming, while i was doing a numerical analysis
|program which uses many for loop
|here is the loop looks like
|
|for(i=1;i<=n;i++);
|{
|}
|
|i put the ";" after the loop header, when the program is excuted, the loop
|is skipped, however, it works fine when the ";" is removed.
|
|I just want to find out why the compiler does not catch it during
|compilation. It is compiled with UNIX gcc.
The compiler can't complain, because your code is perfectly legal
(syntactically speaking that is.) What the compiler actually `sees'
is this: `for(i=1;i<=n;i++)<<empty statement expression>>;' followed
by a block of code. The <<empty statement expression>> is allowed in C.
In this situation it might seem silly to allow this, but have a look
at a naive example implementation of strlen:
size_t strlen(char *s) {
size_t len;
for (len= 0; *s; len++, s++);
return len;
Quote:
}
See? All the necessary actions are done _within_ the for loop part, i.e.
no other statements are necessary here. Isn't C wonderful! ;-)
kind regards,