Scope question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • polas

    #1

    Scope question

    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

  • Richard Heathfield

    #2
    Re: Scope question

    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 */


    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • jacob navia

      #3
      Re: Scope question

      polas wrote:
      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.

      Comment

      • polas

        #4
        Re: Scope question

        On 20 Sep, 10:15, jacob navia <ja...@jacob.re mcomp.frwrote:
        polas wrote:
        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.
        Thats great thanks for the help Richard and Jacob - I never realised
        that a block could be created that way

        Nick

        Comment

        • Philip Potter

          #5
          Re: Scope question

          polas wrote:
          On 20 Sep, 10:15, jacob navia <ja...@jacob.re mcomp.frwrote:
          >polas wrote:
          >>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.
          >
          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

          --
          Philip Potter pgp <atdoc.ic.ac. uk

          Comment

          • jacob navia

            #6
            Re: Scope question

            Philip Potter wrote:
            polas wrote:
            >On 20 Sep, 10:15, jacob navia <ja...@jacob.re mcomp.frwrote:
            >>polas wrote:
            >>>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.
            >>
            >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
            >
            Well, he *is* an authority in technical things. I would not discuss
            that.

            Comment

            • Richard Heathfield

              #7
              Re: Scope question

              Philip Potter said:

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

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Richard

                #8
                Re: Scope question

                Richard Heathfield <rjh@see.sig.in validwrites:
                Philip Potter said:
                >
                <snip>
                >
                >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.
                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.

                Comment

                • Army1987

                  #9
                  Re: Scope question

                  On Thu, 20 Sep 2007 14:21:53 +0200, Richard wrote:
                  Richard Heathfield <rjh@see.sig.in validwrites:
                  >
                  >Philip Potter said:
                  >>
                  ><snip>
                  >>
                  >>It's nice when Richard and Jacob agree with each other.
                  >>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 .
                  >
                  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...
                  --
                  Army1987 (Replace "NOSPAM" with "email")
                  If you're sending e-mail from a Windows machine, turn off Microsoft's
                  stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                  characters through your mail. -- Eric S. Raymond and Rick Moen

                  Comment

                  • Army1987

                    #10
                    Re: Scope question

                    On Thu, 20 Sep 2007 11:38:11 +0000, Richard Heathfield wrote:
                    Philip Potter said:
                    >
                    <snip>
                    >
                    >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...
                    --
                    Army1987 (Replace "NOSPAM" with "email")
                    If you're sending e-mail from a Windows machine, turn off Microsoft's
                    stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                    characters through your mail. -- Eric S. Raymond and Rick Moen

                    Comment

                    • Richard Heathfield

                      #11
                      Re: Scope question

                      Army1987 said:
                      On Thu, 20 Sep 2007 11:38:11 +0000, Richard Heathfield wrote:
                      >
                      >Philip Potter said:
                      >>
                      ><snip>
                      >>
                      >>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.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Scope question

                        Army1987 said:
                        On Thu, 20 Sep 2007 14:21:53 +0200, Richard wrote:
                        >
                        <snip>
                        >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?
                        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>

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Army1987

                          #13
                          Re: Scope question

                          On Thu, 20 Sep 2007 15:39:55 +0000, Richard Heathfield wrote:
                          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.
                          --
                          Army1987 (Replace "NOSPAM" with "email")
                          If you're sending e-mail from a Windows machine, turn off Microsoft's
                          stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                          characters through your mail. -- Eric S. Raymond and Rick Moen

                          Comment

                          Working...