Strcpy versus Strncpy in arrays

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

    #31
    Re: Strcpy versus Strncpy in arrays

    On 2005-11-16, Flash Gordon <spam@flash-gordon.me.uk> wrote:[color=blue]
    > Jordan Abel wrote:[color=green]
    >> On 2005-11-15, Mark McIntyre <markmcintyre@s pamcop.net> wrote:[color=darkred]
    >>> On Tue, 15 Nov 2005 05:48:36 +0000 (UTC), in comp.lang.c , Jordan Abel
    >>> <jmabel@purdue. edu> wrote:
    >>>
    >>>> On 2005-11-14, Mark McIntyre <markmcintyre@s pamcop.net> wrote:
    >>>>> On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
    >>>>> <kst-u@mib.org> wrote:
    >>>>>
    >>>>>> I agree with "Default User". I suppose I have a slight preference for
    >>>>>> postincrement, but I don't find either one counterintuitiv e or
    >>>>>> jarring,
    >>>>> A heck of a lot of learners do however, IME. YMMV etc. You know and I
    >>>>> know that the ++ is evaluated, but the original value of i used in the
    >>>>> loop body. My experience of trainees is that they find that odd.
    >>>> Do what? the ++ is evaluated at the end of the loop body, and the
    >>>> variable's new value is used both for the comparison and for the next
    >>>> instance of the loop body. Where after the increment is the "original
    >>>> value" used?
    >>> Inside the body of the loop. Are you hard of reading?[/color]
    >>
    >> No, the new value is used. The increment happens at the end of the
    >> loop, and then the new value is used for the next execution of the
    >> body. At no time after it is incremented is the previous value used.[/color]
    >
    > I believe that Mark is talking about how people might miss-read the
    > code, not how it actually works. I'm sure Mark knows how it actually works.[/color]

    I read him as claiming that it works this way and that using ++i
    obfuscates how it uses the previous value.
    [color=blue]
    >
    > Beginner known that i++ returns the value from before the increment.
    > Therefore beginner reads the third statement as meaning that the old
    > value of i is used in the body of the loop.
    > Therefore beginner has got it wrong.[/color]

    How can this be used to claim that ++i is less clear, when ++i would in
    that case lead to the correct conclusion?
    [color=blue]
    > In the above none of us are the beginner because we all know better.[/color]

    Comment

    • Mark McIntyre

      #32
      Re: Strcpy versus Strncpy in arrays

      On Tue, 15 Nov 2005 23:40:07 GMT, in comp.lang.c , pete
      <pfiland@mindsp ring.com> wrote:
      [color=blue]
      >It looks to me like he's talking about the significance
      >of the value of expr-3,[/color]

      I'm not talking about any sort of significance.

      Seemingly you're unable to discern the difference between perception
      and knowledge.

      Lets try a simpler example: the ancients thought frogs 'grew' from
      mud, because after heavy rain, frogs were to be seen arising from the
      puddles. We knowledgable people know this to be false, but we can
      perhaps see why a people with less knowledge might be misled.
      --
      Mark McIntyre
      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

      ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

      Comment

      • Mark McIntyre

        #33
        Re: Strcpy versus Strncpy in arrays

        On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
        <spam@flash-gordon.me.uk> wrote:
        [color=blue]
        >I believe that Mark is talking about how people might miss-read the
        >code, not how it actually works. I'm sure Mark knows how it actually works.[/color]

        At last. Somebody who is actually bothering to think ,as opposed to
        merely restate segments of the Standard.
        [color=blue]
        >In the above none of us are the beginner because we all know better.[/color]

        Precisely.
        --
        Mark McIntyre
        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        • Keith Thompson

          #34
          Re: Strcpy versus Strncpy in arrays

          Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
          > On Tue, 15 Nov 2005 23:40:07 GMT, in comp.lang.c , pete
          > <pfiland@mindsp ring.com> wrote:[color=green]
          >>It looks to me like he's talking about the significance
          >>of the value of expr-3,[/color]
          >
          > I'm not talking about any sort of significance.
          >
          > Seemingly you're unable to discern the difference between perception
          > and knowledge.
          >
          > Lets try a simpler example: the ancients thought frogs 'grew' from
          > mud, because after heavy rain, frogs were to be seen arising from the
          > puddles. We knowledgable people know this to be false, but we can
          > perhaps see why a people with less knowledge might be misled.[/color]

          Ok, but we don't carefully remove frogs from mud to avoid confusing
          any ancients who might see them.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Keith Thompson

            #35
            Re: Strcpy versus Strncpy in arrays

            Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
            > On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
            > <spam@flash-gordon.me.uk> wrote:
            >[color=green]
            >>I believe that Mark is talking about how people might miss-read the
            >>code, not how it actually works. I'm sure Mark knows how it actually works.[/color]
            >
            > At last. Somebody who is actually bothering to think ,as opposed to
            > merely restate segments of the Standard.
            >[color=green]
            >>In the above none of us are the beginner because we all know better.[/color]
            >
            > Precisely.[/color]

            Mark, please don't assume that we're all idiots because we
            misunderstand or disagree with what you're saying.

            So, given these two lines of code:

            for (i = 0; i < 10; i++) { ... }
            for (i = 0; i < 10; ++i) { ... }

            we're all in agreement that they're semantically identical (because
            expr-3, either "i++ or "++i", is evaluated only for its side effects,
            with the result being discarded).

            The point on which we disagree is your claim that one form is more
            intuitive than the other. I think a lot of people have misconstrued
            what you've said because, frankly, it doesn't seem to us to make a
            whole lot of sense.

            The most likely explanation for thinking that i++ is better than ++i
            in this context would be a mistaken belief that there's a semantic
            difference. I understand that you do understand the semantics, and
            that that's not the reason for your preference, but you haven't been
            very quick to state this explicitly.

            If you happen to have a personal preference here, that's fine. In
            this particular case, though, you need to be aware that not only will
            most others not share your preference, most of us won't even
            understand the basis for it, and it's not because we're not bothering
            to think.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Jordan Abel

              #36
              Re: Strcpy versus Strncpy in arrays

              On 2005-11-17, Keith Thompson <kst-u@mib.org> wrote:[color=blue]
              > Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
              >> On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
              >> <spam@flash-gordon.me.uk> wrote:
              >>[color=darkred]
              >>>I believe that Mark is talking about how people might miss-read the
              >>>code, not how it actually works. I'm sure Mark knows how it actually works.[/color]
              >>
              >> At last. Somebody who is actually bothering to think ,as opposed to
              >> merely restate segments of the Standard.
              >>[color=darkred]
              >>>In the above none of us are the beginner because we all know better.[/color]
              >>
              >> Precisely.[/color]
              >
              > Mark, please don't assume that we're all idiots because we
              > misunderstand or disagree with what you're saying.
              >
              > So, given these two lines of code:
              >
              > for (i = 0; i < 10; i++) { ... }
              > for (i = 0; i < 10; ++i) { ... }
              >
              > we're all in agreement that they're semantically identical (because
              > expr-3, either "i++ or "++i", is evaluated only for its side effects,
              > with the result being discarded).
              >
              > The point on which we disagree is your claim that one form is more
              > intuitive than the other. I think a lot of people have misconstrued
              > what you've said because, frankly, it doesn't seem to us to make a
              > whole lot of sense.
              >
              > The most likely explanation for thinking that i++ is better than ++i
              > in this context would be a mistaken belief that there's a semantic
              > difference. I understand that you do understand the semantics, and
              > that that's not the reason for your preference, but you haven't been
              > very quick to state this explicitly.
              >
              > If you happen to have a personal preference here, that's fine. In
              > this particular case, though, you need to be aware that not only will
              > most others not share your preference, most of us won't even
              > understand the basis for it, and it's not because we're not bothering
              > to think.[/color]

              I think that it fundamentally comes down to it being what he learned
              first. It's convention, plain and simple. I'll usually use i++ instead
              of ++i too, because it's what I learned.

              Comment

              • pete

                #37
                Re: Strcpy versus Strncpy in arrays

                Jordan Abel wrote:
                [color=blue]
                > I think that it fundamentally comes down to it being what he learned
                > first. It's convention, plain and simple. I'll usually use i++ instead
                > of ++i too, because it's what I learned.[/color]

                I actually switched my preference from i++ to ++i,
                for expressions evaluated as void.

                I consider the standard description of postfix increment
                to be more complicated, than that of prefix increment.
                Not much more complicated, just a little.

                The point being, that I would need some reason
                to use the conceptually more complicated increment.
                A void expression evaluation provides no such reason.

                When I see a postincremented void evaluted expression
                in somebody else's code, I tend not to comment on it.
                If I rewrite somebody else's posted code and post it,
                there's a good chance that
                I'll change postincrented void evaluated expressions
                to preincremented, because then it's my code.

                My preference may also be influenced by K&R writing one way
                and Schildt writing the other.

                I originally learned C by Schildt,
                in three weeks!!!
                and again over the course of years
                in an ongoing process here on clc.

                --
                pete

                Comment

                • Keith Thompson

                  #38
                  Re: Strcpy versus Strncpy in arrays

                  pete <pfiland@mindsp ring.com> writes:[color=blue]
                  > Jordan Abel wrote:[color=green]
                  >> I think that it fundamentally comes down to it being what he learned
                  >> first. It's convention, plain and simple. I'll usually use i++ instead
                  >> of ++i too, because it's what I learned.[/color]
                  >
                  > I actually switched my preference from i++ to ++i,
                  > for expressions evaluated as void.
                  >
                  > I consider the standard description of postfix increment
                  > to be more complicated, than that of prefix increment.
                  > Not much more complicated, just a little.
                  >
                  > The point being, that I would need some reason
                  > to use the conceptually more complicated increment.
                  > A void expression evaluation provides no such reason.[/color]

                  Fair enough.

                  Personally, I don't even think about the distinction between i++ and
                  ++i if it appears in a void context, and I really have no preference
                  one way or the other. If the result is used, I have to stop and think
                  about what's going on, but I actually think both forms are used more
                  often in void context than otherwise.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Mark McIntyre

                    #39
                    Re: Strcpy versus Strncpy in arrays

                    On Thu, 17 Nov 2005 00:50:03 GMT, in comp.lang.c , Keith Thompson
                    <kst-u@mib.org> wrote:
                    [color=blue]
                    >Mark, please don't assume that we're all idiots because we
                    >misunderstan d or disagree with what you're saying.[/color]

                    I don't. I have however been forced to the conclusion that several of
                    you are suffering from a bad case of "the other guy must be an idiot
                    because he doesn't understand the simple rules of
                    reverse-polish-flombat swarfling with the added bermudan option, but
                    disallowing cue bidding"
                    [color=blue]
                    >The point on which we disagree is your claim that one form is more
                    >intuitive than the other. I think a lot of people have misconstrued
                    >what you've said because, frankly, it doesn't seem to us to make a
                    >whole lot of sense.[/color]

                    See above.
                    --
                    Mark McIntyre
                    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                    Comment

                    • Mark McIntyre

                      #40
                      Re: Strcpy versus Strncpy in arrays

                      On Thu, 17 Nov 2005 02:13:14 +0000 (UTC), in comp.lang.c , Jordan Abel
                      <jmabel@purdue. edu> wrote:
                      [color=blue]
                      >I think that it fundamentally comes down to it being what he learned
                      >first.[/color]

                      I learned both at the same time.

                      As it happens, I generally use ++var when I want to preincrement a
                      variable, and var++ when I want to postincrement it.
                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                      ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                      Comment

                      • Keith Thompson

                        #41
                        Re: Strcpy versus Strncpy in arrays

                        Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                        > On Thu, 17 Nov 2005 02:13:14 +0000 (UTC), in comp.lang.c , Jordan Abel
                        > <jmabel@purdue. edu> wrote:
                        >[color=green]
                        >>I think that it fundamentally comes down to it being what he learned
                        >>first.[/color]
                        >
                        > I learned both at the same time.
                        >
                        > As it happens, I generally use ++var when I want to preincrement a
                        > variable, and var++ when I want to postincrement it.[/color]

                        But that's not the whole story. And if you just want to increment a
                        variable, in a context where there's no difference between
                        preincrement and postincrement, you seem to have a strong preference
                        for one of them (I don't remeber which). That's what we found
                        confusing.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Mark McIntyre

                          #42
                          Re: Strcpy versus Strncpy in arrays

                          On Sat, 19 Nov 2005 18:48:01 GMT, in comp.lang.c , Keith Thompson
                          <kst-u@mib.org> wrote:
                          [color=blue]
                          >you seem to have a strong preference
                          >for one of them (I don't remeber which).[/color]

                          Not at all - I do however have a strong preference for clarity, and in
                          my experience, learners often have misunderstandin gs that can be
                          avoided by steering clear of potentially confusing constructs. For
                          instance, its often said that
                          if( 4 == somevariable )
                          is safer than
                          if(somevariable == 4)
                          because it avoids the possible mistake of the single equals. It is
                          however quite counterintuitiv e at first glance. Thus, while I do
                          believe the former is safer, I rarely recommend it.
                          [color=blue]
                          > That's what we found confusing.[/color]

                          I strongly suspect that what you found confusing was that to you,
                          these rules are *obvious* and self-explanatory, you couldn't
                          understand why anyone would find them otherwise, and therefore my
                          remark that the construct could confuse was itself baffling to you.

                          Sometimes one needs to step back from ones knowledge.
                          --
                          Mark McIntyre
                          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                          ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                          Comment

                          • Joe Wright

                            #43
                            Re: Strcpy versus Strncpy in arrays

                            Mark McIntyre wrote:[color=blue]
                            > On Sat, 19 Nov 2005 18:48:01 GMT, in comp.lang.c , Keith Thompson
                            > <kst-u@mib.org> wrote:
                            >
                            >[color=green]
                            >>you seem to have a strong preference
                            >>for one of them (I don't remeber which).[/color]
                            >
                            >
                            > Not at all - I do however have a strong preference for clarity, and in
                            > my experience, learners often have misunderstandin gs that can be
                            > avoided by steering clear of potentially confusing constructs. For
                            > instance, its often said that
                            > if( 4 == somevariable )
                            > is safer than
                            > if(somevariable == 4)
                            > because it avoids the possible mistake of the single equals. It is
                            > however quite counterintuitiv e at first glance. Thus, while I do
                            > believe the former is safer, I rarely recommend it.
                            >
                            >[color=green]
                            >>That's what we found confusing.[/color]
                            >
                            >
                            > I strongly suspect that what you found confusing was that to you,
                            > these rules are *obvious* and self-explanatory, you couldn't
                            > understand why anyone would find them otherwise, and therefore my
                            > remark that the construct could confuse was itself baffling to you.
                            >
                            > Sometimes one needs to step back from ones knowledge.[/color]

                            I write C as I say it. 'If var is 4' is written..

                            if (var == 4)

                            I never confuse == and = in these expressions. If I did, either the
                            compiler will tell me or the test results of the program will.

                            for (i = 0; i < N; ++i)

                            Again I say it "increment i" and therefore '++i'.

                            --
                            Joe Wright
                            "Everything should be made as simple as possible, but not simpler."
                            --- Albert Einstein ---

                            Comment

                            • Skarmander

                              #44
                              Re: Strcpy versus Strncpy in arrays

                              Joe Wright wrote:
                              <snip>[color=blue]
                              > I write C as I say it. 'If var is 4' is written..
                              >
                              > if (var == 4)
                              >
                              > I never confuse == and = in these expressions. If I did, either the
                              > compiler will tell me or the test results of the program will.
                              >
                              > for (i = 0; i < N; ++i)
                              >
                              > Again I say it "increment i" and therefore '++i'.
                              >[/color]
                              And you never confuse ++i and i++ in these expressions, because the
                              compiler or the results will tell you...

                              Oh, and how do you pronounce "i |= 0x80", by the way? It's either going
                              to be a circumscription or something that's not English. I "say" this as
                              "i or-is oh ex eighty", but I don't assign any significance to this --
                              I'm just reading the symbols. If I were going from English to code, I'd
                              probably say "set the eighth/high/sign bit of i", depending on i.

                              "Increment i" does not have the unique translation "++i". Saying that
                              this is "the way you say it" doesn't mean anything more than "I just
                              prefer ++i". This "I write it as I say it" argument just muddies the
                              waters; it's no more or less arbitrary than anything.

                              The reason most of us don't write (4 == var) regardless of safety is
                              because it's just not written that way outside C. Open any textbook on
                              mathematics, sit in any class where equations are written down, and
                              you'll find that equalities are never written with a constant on the
                              left hand side and a variable on the right. That, in turn, is a result
                              of left-to-right writing in English, and the way equations are typically
                              solved. How we say it has little to do with it.

                              I've said "increment i" to "i = i + 1", not "i becomes equal to the old
                              value of i plus one" or suchlike. Of course I'd never write "increment
                              i" as "i = i + 1" in C, but whether I'll use ++i or i++ depends on the
                              expression.

                              If either will do, I'll use ++i, because this is what I'm used to from
                              (irony) C++. Of course I have no trouble reading any of these
                              expressions in an abstract sense, and nobody should. And if I can avoid
                              it, I won't try to express them in English either. English sucks for
                              writing programs in, though it's excellent at pseudocode.

                              S.

                              Comment

                              • Markus Becker

                                #45
                                Re: Strcpy versus Strncpy in arrays

                                On 2005-11-19, Skarmander <invalid@dontma ilme.com> wrote:
                                [color=blue]
                                > Oh, and how do you pronounce "i |= 0x80", by the way? It's either going[/color]

                                My german 'expression' for "i |= 0x80" is "i oder-gleich Null-x-Achtzig"
                                which would translate to (how is "=" pronounced in english?)
                                "i or-assign zero-x-eighty".
                                [color=blue]
                                > to be a circumscription or something that's not English. I "say" this as
                                > "i or-is oh ex eighty", but I don't assign any significance to this --[/color]

                                Ah, I was almost right. :-)
                                [color=blue]
                                > "Increment i" does not have the unique translation "++i". Saying that
                                > this is "the way you say it" doesn't mean anything more than "I just
                                > prefer ++i". This "I write it as I say it" argument just muddies the
                                > waters; it's no more or less arbitrary than anything.[/color]

                                It depends on the perspective.
                                [color=blue]
                                > I've said "increment i" to "i = i + 1", not "i becomes equal to the old
                                > value of i plus one" or suchlike. Of course I'd never write "increment
                                > i" as "i = i + 1" in C, but whether I'll use ++i or i++ depends on the
                                > expression.[/color]

                                Sure it should. There's quite a difference between "a[++i] = 42" and
                                "a[i++] = 42". Obviously not in terms of "i", but in terms of "a[]".

                                But you all know that.

                                Markus

                                Comment

                                Working...