bug of vs2k5?

A

abracadabra

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.
 
M

Mike Wahler

abracadabra said:
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
 
A

abracadabra

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.
 
A

Andrey Tarasevich

abracadabra said:
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.
 
J

John L Fjellstad

abracadabra said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top