|
Redeclaration of local variables not caught
#417266
02/11/13 02:41
02/11/13 02:41
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Hello jcl, the following code should be rejected with an error message but instead compiles and executes:
#include <acknex.h>
void main()
{
int i = 0;
int i = 0;
}
Best regards, Uhrwerk
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Redeclaration of local variables not caught
[Re: jcl]
#417323
02/11/13 15:49
02/11/13 15:49
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Ok. I didn't find this case specified in the C Reference Manual. So I tried some other compilers and they all gave an error message similar to "error: redeclaration of 'i'". What's the purpose of redeclaring local variables?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Redeclaration of local variables not caught
[Re: Uhrwerk]
#417335
02/11/13 17:25
02/11/13 17:25
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
oh, redeclaring is allowerd and mostly results in this spaghetie source:
for(int i = 0; i < 10; i++)
{
for(int i = 0; i < 100; i++)
{
dosomethingwithint(i);
}
}
I hate nested loops with redeclared vars ARGH -.-
Last edited by Rackscha; 02/11/13 17:25.
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: Redeclaration of local variables not caught
[Re: Rackscha]
#417336
02/11/13 17:28
02/11/13 17:28
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
That's a different example. In your code snippet two variables with identical name are declared as well, but they have got a different scope.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Redeclaration of local variables not caught
[Re: Uhrwerk]
#417342
02/11/13 19:35
02/11/13 19:35
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
@Uhrwerk: yes they have different scopse, nontheless: Redeclaring the same identifier over and over again just starts to confuse other people when reading the source
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: Redeclaration of local variables not caught
[Re: Rackscha]
#417351
02/11/13 20:37
02/11/13 20:37
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
I absolutely agree. Nevertheless this is not related to the original topic.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|