By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

bug of vs2k5?

P: n/a
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).

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. I don't know if this
has been fixed in Service Pack 1 of Visual Studio 2005. And I am
wondering the result of Visual C++ 6.0 and Visual Studio.net 2003.

Dec 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"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
Dec 27 '06 #2

P: n/a
Thanks a lot. I've corrected this by moving the declaration to the
beginning. After changing the postfix from ".c" to ".cpp", the code
compiles. This support your point of C++. Maybe GCC makes an extension
to C99 in this case. There's no warning/error like "missing ';' before
'type'...", even gcc -Wall is specified.

Dec 27 '06 #3

P: n/a
abracadabra wrote:
Maybe GCC makes an extension
to C99 in this case. There's no warning/error like "missing ';' before
'type'...", even gcc -Wall is specified.
Yes, GCC has an extension that allows such declarations. It is not an
extension to C99 though, since C99 specifically allows them as well. It
is an extension to C89/90.

If you use GCC with '-ansi -pedantic' it will give you a warning about
that (maybe just '-pedantic' is enough, I don't remember exactly). Just
'-Wall' is not enough.

--
Best regards,
Andrey Tarasevich

Dec 27 '06 #4

P: n/a
"abracadabra" <je*********@yahoo.comwrites:
Thanks a lot. I've corrected this by moving the declaration to the
beginning. After changing the postfix from ".c" to ".cpp", the code
compiles. This support your point of C++. Maybe GCC makes an extension
to C99 in this case. There's no warning/error like "missing ';' before
'type'...", even gcc -Wall is specified.
I think you have to add -ansi to remove GNU extensions in GCC.

--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes
Replace YEAR with current four digit year
Dec 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.