Scope question

J

jacob navia

polas said:
Morning all. I have a question - I am really not sure about the answer
and hope that it is not compiler specific... I am writing a language
translator to translate into C. There are some additional scoping
issues in the language which I would like to preserve in the C code.
Because of this I was wondering if there is a "good" way of creating a
scoped block in C - of course I could use a dummy conditional or
something like that, but I was wondering if there is a better way.

Cheers,
Nick

Anywhere in your code you can create a block scope.

....
{
local variables
statements
}

the local variables in that block will be only visible
in that bloack. You do not need a conditional to open
a block.
 
P

polas

Morning all. I have a question - I am really not sure about the answer
and hope that it is not compiler specific... I am writing a language
translator to translate into C. There are some additional scoping
issues in the language which I would like to preserve in the C code.
Because of this I was wondering if there is a "good" way of creating a
scoped block in C - of course I could use a dummy conditional or
something like that, but I was wondering if there is a better way.

Cheers,
Nick
 
R

Richard Heathfield

polas said:
Morning all. I have a question - I am really not sure about the answer
and hope that it is not compiler specific... I am writing a language
translator to translate into C. There are some additional scoping
issues in the language which I would like to preserve in the C code.
Because of this I was wondering if there is a "good" way of creating a
scoped block in C - of course I could use a dummy conditional or
something like that, but I was wondering if there is a better way.

{
}

For example:

int foo(void)
{
int i; /* i is in scope here */

{ /* this introduces a new scope, and you don't need
a dummy conditional. */


int i; /* this i is in scope here, and the other one is
no longer visible because this one masks it */


/* this i is about to go out of scope */
}

/* the first i is now visible again */
 
P

polas

Anywhere in your code you can create a block scope.

...
{
local variables
statements

}

the local variables in that block will be only visible
in that bloack. You do not need a conditional to open
a block.

Thats great thanks for the help Richard and Jacob - I never realised
that a block could be created that way

Nick
 
J

jacob navia

Philip said:
It's nice when Richard and Jacob agree with each other. It doesn't seem
like it happens very often round here...

Phil

Well, he *is* an authority in technical things. I would not discuss
that.
 
P

Philip Potter

polas said:
Thats great thanks for the help Richard and Jacob - I never realised
that a block could be created that way

It's nice when Richard and Jacob agree with each other. It doesn't seem like it
happens very often round here...

Phil
 
R

Richard Heathfield

Philip Potter said:

It's nice when Richard and Jacob agree with each other.

Don't count on it. I haven't seen Mr Navia's reply, so I can't tell whether
I agree with him or not.
It doesn't seem like it happens very often round here...

Since he's in my killfile, that's not very surprising. He's there because
he was wrong so often that people got fed up with my correcting him. Now
that I've killfiled him, /they/ have to correct him - which they have had
to do rather a lot.

I'd be surprised (and delighted) if his reply in this thread were devoid of
inaccuracies.
 
R

Richard

Richard Heathfield said:
Philip Potter said:



Don't count on it. I haven't seen Mr Navia's reply, so I can't tell whether
I agree with him or not.


Since he's in my killfile, that's not very surprising. He's there because
he was wrong so often that people got fed up with my correcting him. Now
that I've killfiled him, /they/ have to correct him - which they have had
to do rather a lot.

I'd be surprised (and delighted) if his reply in this thread were devoid of
inaccuracies.

It is such a shame that someone with your undoubted skills in C should
take such delight in being such a vindictive person. Jacob is frequently
helpful and practical to many posters. He doesn't sneer or talk down to
anyone. Should you ever make that position available, I doubt if he
would take it.
 
A

Army1987

It is such a shame that someone with your undoubted skills in C should
take such delight in being such a vindictive person. Jacob is frequently
helpful and practical to many posters.
Is he? He's in my killfile too, but in the stuff I read written by
him and quoted by others, the one in this thread are the first
ones really useful (for someone who doesn't have lcc-win32, at
least) since weeks.
He doesn't sneer or talk down to anyone.
Doesn't he anymore? Good news...
 
A

Army1987

Philip Potter said:

It's nice when Richard and Jacob agree with each other.
[snip]
I'd be surprised (and delighted) if his reply in this thread were devoid of
inaccuracies.
To be honest (because I am too good a person, rather than because
I think he deserves that), I temporarily turned the filter off,
and I saw that JN's post was entirely quoted by polas. Apart from
a typo there is nothing wrong with it. Strange but true...
 
R

Richard Heathfield

Army1987 said:
Philip Potter said:

It's nice when Richard and Jacob agree with each other.
[snip]
I'd be surprised (and delighted) if his reply in this thread were devoid
of inaccuracies.
To be honest (because I am too good a person, rather than because
I think he deserves that), I temporarily turned the filter off,
and I saw that JN's post was entirely quoted by polas. Apart from
a typo there is nothing wrong with it. Strange but true...

If that is the case (and Polas has omitted nothing from that quote), then
the reply is indeed inaccurate, as I had feared, although it was such a
trivial question that it was difficult to get it terribly wrong.

Mr Navia's claim, "Anywhere in your code you can create a block scope." is
simply not true, and it is so trivial to demonstrate this that I would not
think it inappropriate to hand the proof to first-week C students as an
exercise.
 
R

Richard Heathfield

Army1987 said:
On Thu, 20 Sep 2007 14:21:53 +0200, Richard wrote:

Is he?

No, but then neither is Richard Riley. Last I checked, he restricted his
activities to criticising the helpful rather than trying to be helpful
himself. For that reason, Mr Riley, too, is in my killfile. From your
quote, it is evident that this does not prevent him making lousy
deductions and outrageous claims, but hey, life's too short to spend it
fighting with bozos.

<snip>
 
A

Army1987

Mr Navia's claim, "Anywhere in your code you can create a block scope." is
simply not true, and it is so trivial to demonstrate this that I would not
think it inappropriate to hand the proof to first-week C students as an
exercise.

Probably he didn't really mean *that* by "anywhere", but...
Ok, finding something correct written by him is even harder than
I imagined.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top