"abracadabra" <je*********@yahoo.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
Hi all. I am working on some string manipulation, and I want the passed
in string pointer to be checked. So I use "assert" to assure that. But
the C compiler of Visual Studio 2005 fails to compile that, if there's
a variable declaration after an assertion in a function(except for the
"main" function).
The C compiler in Visual Studio 2005 conforms (mostly) with the
ISO/ANSI C Standard 9899:1990. This standard does not allow variable
declarations other than at the beginning of a block or at file scope.
Your code violates this standard.
Move your declaration of 'int a' to the beginning of the 'donothing()'
function, and it will compile.
Here goes the code,
/************************begin of test code**********************/
#include <stdio.h>
#include <assert.h>
char *donothing(char *test)
{
assert(test!=NULL);
int a;
return test;
}
int main()
{
char *str = "abc";
donothing(str);
return 0;
}
/**************************end of test code********************/
The same code can be compiled successfully by GCC.
That's because of one of two possibilites:
1) GCC supplies an extension to the C90 language which allows the above
code.
2) Later versions of GCC support the newer (1999) C standard, which
does support variable declarations as above.
Check the documentation and the options you're using with that compiler.
I don't know if this
has been fixed in Service Pack 1 of Visual Studio 2005.
There's nothing to fix with regard to your above code and that compiler.
The compiler correctly reported the error.
And I am
wondering the result of Visual C++ 6.0 and Visual Studio.net 2003.
The C compiler provided with Visual C++ will give the same result.
I have no experience with Visual Studio .NET. I don't know if it
supports C99 or not.
Finally, note that the C++ language does support what you're
doing, and that this newsgroup is for discussion of the C++ language,
not the C language, the fact that most C++ packages contain C compilers
notwithstanding.
-Mike