Re-declaration !!

K

karthikbalaguru

Hi,

I am eager to know the advantage of support for
redeclarations between different blocks w.r.t block level
within in the same function?
Where could this feature of C language be
very useful ?

Thx in advans,
Karthik Balaguru
 
F

Falcon Kirtaran

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

I am eager to know the advantage of support for
redeclarations between different blocks w.r.t block level
within in the same function?
Where could this feature of C language be
very useful ?

Thx in advans,
Karthik Balaguru

Do you mean what is the use of:

int main() {
int x = 100;
int y = 0;
while (x) {
int y = 0;
--x;
};
};

There isn't really much use, unless you want to re-use the variable name
for some reason.

- --
- --Falcon Darkstar Christopher Momot
- --
- --OpenPGP: (7902:4457) 9282:A431

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJJtcIvAAoJEKmxP9YxEE4rACsQAMhrHhgf+YVETm6Wmn+YN5JP
qgLXTuSWJOMEHVeE7nGTRceDbb53w1/NQsBCDQwZ1ST9Eu+b9qVJePg4IbsF+yCM
yjwHJmjzONK/sNFCDEtfbzRWWQ4bgcZswD3AZkuiINZ8cPvM52F4QWB2f6ops36h
lxBTLNcHz/4498i8I/v39nkWcaqbJ4c4lqak1Y+Vp4lcHzvh0XzDKndbmdUUr144
yyv0VevqYlMqHdeM2aG5ttPpivna05CK1k9ISq8p1bEorY3PuBkZOLCh3wKRyD21
cjB54wjM7HIEPA7ejmKqiFjWoIMzTNYSuaKAkaniVm9LVaRVZPRnV6uzM6W4o+Ml
Ed8dvrYwkD0RyzgY8VUJ6sT1gdLddftcrdC/JSVpQaTor8q0UR4u5/ZsaAkxbBOg
DcZK/6he7MKEhCFF3pK1mF6Npcz/0v8MXxZeqCJTvqDEVDPboIUpwaVAnuAAS8zF
avzfP3bRJMCni1d5Ei2pwHEfgqlh+whR6XafwI3L2rxGIFTM+wpoftnBRP7rps1S
0G/IpXExHFvup98W+GbGG/o+wQRylEVVEoIrhnGAWopYtPqUeWM7M325DL1tuwWT
Guw/jlHf24DDT84pWHPS/RtN++eEpnl+3KCM7btuphIiuhYRoSbOCreTT8kSjpsC
TK13ErQMFtN7C7L1JPR5
=Zv/C
-----END PGP SIGNATURE-----
 
D

Dik T. Winter

> I am eager to know the advantage of support for
> redeclarations between different blocks w.r.t block level
> within in the same function?
> Where could this feature of C language be
> very useful ?

That when you write a block you do not need to consider the identifiers
already used in other blocks. It has been a feature since the first
block-scoped language: Algol 60.
 
K

Kaz Kylheku

Hi,

I am eager to know the advantage of support for
redeclarations between different blocks w.r.t block level
within in the same function?

Strictly speaking, a inner block doesn't redeclare anything in an outer block,
unless the two are names with linkage

{
extern int x; // connect with file scope x

{
extern int x; // same x, redeclared
Where could this feature of C language be
very useful ?

This isn't a feature. It's the lack of a feature: the lack of a prohibition
against one lexical scope using the same name as another lexical scope.

I ask you: how would it be useful to introduce the feature of prohibiting
shadowing definitions?

If you have a block like

{ char *p; ... }

you would not be able to relocate this block around in your program without
being concerned about the problem that the p may clash with something.

Code with lots of lexical shadowing may be generated by programs that write
C code. Such generators would be more complicated if they had to work around
a prohibition; they would have to use gensyms for all local variables, and
know which gensym to use in what situation.
 
K

karthikbalaguru

That when you write a block you do not need to consider the identifiers
already used in other blocks.

But, Is this an efficient approach as re-declaration and the relevant
defintions will cost you space and time ?

Karthik Balaguru
 
K

karthikbalaguru

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1




Do you mean what is the use of:

int main() {
int x = 100;
int y = 0;
while (x) {
int y = 0;
--x;
};

};

There isn't really much use, unless you want to re-use the variable name
for some reason.

Is it just for re-using the same name of a particular variable ?
Interesting !!

Karthik Balaguru
 
C

Chris Dollin

karthikbalaguru said:
On Mar 10, 6:28 am, Falcon Kirtaran

Is it just for re-using the same name of a particular variable ?
Interesting !!

It's not "just for re-using"; it means that you can introduce a
block-local variable /without having to worry about existing
variables of the same name/.

Re-using the same name multiple times in a single function is probably
best avoided; after all, you can rename the locals without effecting
global consequences. But you don't want the introduction of a new global
name to mess up local declarations that happen to have that same
spelling.

(Especially in a language with no global-namespace-control machinery.)
 
D

Dik T. Winter

>
> But, Is this an efficient approach as re-declaration and the relevant
> defintions will cost you space and time ?

Not necessarily. Given:

void f() {
{ int i, j; i = 1; j = 2;}
{ int i; i = 2;}
}

a good compiler will know that the first block contains a declaration for
two ints and the second block for one int. It will allocate the local
variables in the block starting at the same position.
 
G

Guest

I am eager to know the advantage of support for
redeclarations between different blocks w.r.t block level
within in the same function?
Where could this feature of C language be
very useful ?

I usually write quite small functions these days so block scope
variables become less useful. But if I want a block scope variable
I define one. I choose a meaningful name if the name chosen hides
a variable at an outerer scope (and I don't need access to the
original)
then so be it. I must admit I'd tend not to hide another variable
because
(a) it looks ugly (to me) and (b) it seems to confuse people.

If you block scope variables a lot I'd say your functions might be too
big.
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top