Strcpy versus Strncpy in arrays

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

    #46
    Re: Strcpy versus Strncpy in arrays

    Markus Becker wrote:[color=blue]
    > On 2005-11-19, Skarmander <invalid@dontma ilme.com> wrote:[color=green]
    >>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=green]
    >>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]

    Make no mistake, "assign" is actually better than "is" or "gleich",
    because it's *not* equality. But we're used to this by now. The horror
    of languages that use := or similar for assignment! Think of all the
    wasted keystrokes! :-)

    S.

    Comment

    • Old Wolf

      #47
      Re: Strcpy versus Strncpy in arrays

      Joe Wright wrote:[color=blue]
      >
      > I write C as I say it. 'If var is 4' is written..
      >
      > if (var == 4)[/color]

      How do you write:
      p is a pointer to function taking an int
      and returning a pointer to void
      ?

      Comment

      • Joe Wright

        #48
        Re: Strcpy versus Strncpy in arrays

        Old Wolf wrote:[color=blue]
        > Joe Wright wrote:
        >[color=green]
        >>I write C as I say it. 'If var is 4' is written..
        >>
        >> if (var == 4)[/color]
        >
        >
        > How do you write:
        > p is a pointer to function taking an int
        > and returning a pointer to void
        > ?
        >[/color]

        Can I call you by your first name? Old? Maybe..

        void *(*p)(int);

        Close? What was the Wolf's point?

        My original quip was that 'if (4 == var)' is counter intuitive and is
        not used here.

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

        Comment

        • Dave Thompson

          #49
          Re: Strcpy versus Strncpy in arrays

          On Sun, 13 Nov 2005 23:57:55 GMT, pete <pfiland@mindsp ring.com> wrote:
          [color=blue]
          > Mark McIntyre wrote:[/color]
          [color=blue][color=green][color=darkred]
          > > > scanf("%s", &again);
          > > >
          > > > }while(again == 'Y' || again == 'y');[/color]
          > >
          > > again is a string, not a single char so you need "Y" and "y". Or
          > > better yet, declare again as a character and use the %c format.[/color]
          >
          > 'again' was declared as a char. another global[/color]

          So %s, which will (normally) store _at least_ 1 data character plus a
          null terminator character, is UB. Use %c, and perhaps " %c" to skip
          leading whitespace. Better yet, don't use scanf at all: FAQs 12.12-20
          at usual places and http://www.eskimo.com/~scs/C-faq/top.html

          But if it _were_ a string, then just substituting again == "Y" is
          wrong in a different way; FAQ 8.2, or use again[0] == 'Y'.

          - David.Thompson1 at worldnet.att.ne t

          Comment

          • Mabden

            #50
            Re: Strcpy versus Strncpy in arrays

            "Skarmander " <invalid@dontma ilme.com> wrote in message
            news:437fb858$0 $11061$e4fe514c @news.xs4all.nl ...[color=blue]
            > Joe Wright wrote:
            > <snip>[color=green]
            > > 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.[/color][/color]

            I have started using the "if (NO_ERROR == result)" syntax even tho I am
            thoroughly anal in my code (at least, people tell me I'm a "complete
            asshole", so I guess they mean my code is good...)
            [color=blue][color=green]
            > > for (i = 0; i < N; ++i)
            > >
            > > Again I say it "increment i" and therefore '++i'.[/color][/color]

            But in a for() loop, you want to have the idea that i will be plus-one
            in the next loop, so i++ really states the idea better. In the case of
            just incrementing a variable, I use "++j;" because I read it as
            "increment j" as opposed to "j++;" which I read as "do nothing, then
            increment j" which seems silly.

            for (theRecord = Nothing; theRecord < Gold; theRecord++)
            ++sales;
            [color=blue]
            > Oh, and how do you pronounce "i |= 0x80"[/color]

            Set high bit of i.
            [color=blue]
            > I'm just reading the symbols. If I were going from English to code,[/color]
            I'd[color=blue]
            > probably say "set the eighth/high/sign bit of i", depending on i.[/color]

            Sixteenth bit, I'm sure you meant to say...
            [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]

            Agreed. But I like it. "It's only Rock-n-Roll, but I like it..."
            [color=blue]
            > 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[/color]
            on[color=blue]
            > 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[/color]
            typically[color=blue]
            > solved. How we say it has little to do with it.[/color]

            But it IS a Good Idea. I have begun to do it, and I find it really isn't
            the completely inane construct I thought it was. Of course, it scans
            better when the constants are #define'd. It's not:
            if (4 == ever)...

            it's more like:
            if (FILE_LOCKED == result) file_locked (fptr);
            if (FILE_PROTECTED == result) file_prot (fptr);

            So you see, the info important to the programmer is right up front, AND
            the compiler will balk at an error. It's a two-fer. Worth embracing.
            Think on it.

            [color=blue]
            > I've said "increment i" to "i = i + 1", not "i becomes equal to the[/color]
            old[color=blue]
            > 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++.[/color]

            Double-plus good.
            [color=blue]
            > Of course I have no trouble reading any of these
            > expressions in an abstract sense, and nobody should.[/color]

            Nobody should read any of these expressions in an abstract sense? Maybe
            you're right... ;-)

            --
            Mabden


            Comment

            • Skarmander

              #51
              Re: Strcpy versus Strncpy in arrays

              Mabden wrote:[color=blue]
              > "Skarmander " <invalid@dontma ilme.com> wrote in message
              > news:437fb858$0 $11061$e4fe514c @news.xs4all.nl ...
              >[/color]
              <snip>[color=blue][color=green]
              >> Oh, and how do you pronounce "i |= 0x80"[/color]
              >
              > Set high bit of i.
              >[color=green]
              >> 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.[/color]
              >
              > Sixteenth bit, I'm sure you meant to say...
              >[/color]
              No, I did not. Eighth is correct. You could say seventh, if you start
              counting from 0 (which is neat because 1 << 7 == 0x80). "Sixteen is
              right out".

              You're thinking of sixteen-bit integers and their "high bits", aren't you?
              [color=blue][color=green]
              >> 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.[/color]
              >
              > But it IS a Good Idea. I have begun to do it, and I find it really isn't
              > the completely inane construct I thought it was. Of course, it scans
              > better when the constants are #define'd. It's not:
              > if (4 == ever)...
              >
              > it's more like:
              > if (FILE_LOCKED == result) file_locked (fptr);
              > if (FILE_PROTECTED == result) file_prot (fptr);
              >
              > So you see, the info important to the programmer is right up front, AND
              > the compiler will balk at an error. It's a two-fer. Worth embracing.
              > Think on it.
              >[/color]
              Hmm. This particular case study is not so convincing, given:

              switch (result) {
              case FILE_LOCKED: file_locked(fpt r); break;
              case FILE_PROTECTED: file_prot(fptr) ; break;
              default: /* there probably ought to be something here */
              }

              This is one of those arguable things, you know. What info is important
              to the programmer? That "result" is tested? That the symbolic constant
              FILE_LOCKED means that the, eh, file is locked? Well, arguably both.
              Whether you write this one way or the other is insignificant. As long as
              it fits on a line.

              Compare:

              int main(int argc, char* argv);

              function main(argc: integer, argv: array of string): integer;

              Look at the clarity of that last one: first the identifier, then the
              type. What does the programmer care more about, eh? This language could
              be easier to learn and read!

              Maybe. But when you've learned it, does it matter? No. It's easy enough
              to adapt to either.

              The only argument that really matters is that for (constant == variable)
              the compiler will definitely catch the mistake of writing = instead of
              ==; the rest is stylistic quabbles which can go on forever without
              reaching any sort of consensus.

              The benefit of having a guaranteed error (instead of just a warning,
              which many compilers will emit) just doesn't do it for me. I'm not going
              to suddenly write all my comparisons differently in C because the
              language has a gotcha I'm well aware of. If it works for you, great.
              [color=blue][color=green]
              >>Of course I have no trouble reading any of these
              >>expressions in an abstract sense, and nobody should.[/color]
              >
              > Nobody should read any of these expressions in an abstract sense? Maybe
              > you're right... ;-)
              >[/color]

              Now I'm pretty sure the grammar doesn't allow for that meaning...

              S.

              Comment

              • Michael Wojcik

                #52
                Re: Strcpy versus Strncpy in arrays


                In article <slrndnvk2l.h5n .yetispamb@beta-cygni.homelinux .org>, Markus Becker <yetispamb@web. de> writes:[color=blue]
                >
                > (how is "=" pronounced in english?)[/color]

                Like "-", but doubled.

                Seriously, it depends on the speaker, of course. I rarely say it
                aloud; when I do, I usually say "equals", even when it's being used
                for assignment. If I needed to describe C source code orally and
                precisely - if I were giving a lecture to nonexperts, perhaps - I
                would probably say "gets" (an elision of "gets the value" or "gets
                the value of"). With "gets", the LHS of the expression can serve
                as the grammatical subject of the clause ("x gets y" for "x = y"),
                which is not the case with verbs like "assign".

                --
                Michael Wojcik michael.wojcik@ microfocus.com

                Art is our chief means of breaking bread with the dead ... but the social
                and political history of Europe would be exactly the same if Dante and
                Shakespeare and Mozart had never lived. -- W. H. Auden

                Comment

                • Jordan Abel

                  #53
                  Re: Strcpy versus Strncpy in arrays

                  On 2005-11-22, Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
                  > "Skarmander " <invalid@dontma ilme.com> wrote in message
                  > news:437fb858$0 $11061$e4fe514c @news.xs4all.nl ...[color=green]
                  >> Joe Wright wrote:[color=darkred]
                  >> > for (i = 0; i < N; ++i)
                  >> >
                  >> > Again I say it "increment i" and therefore '++i'.[/color][/color]
                  >
                  > But in a for() loop, you want to have the idea that i will be plus-one
                  > in the next loop, so i++ really states the idea better. In the case of
                  > just incrementing a variable, I use "++j;" because I read it as
                  > "increment j" as opposed to "j++;" which I read as "do nothing, then
                  > increment j" which seems silly.[/color]

                  Eh? That doesn't make sense.

                  Comment

                  • Netocrat

                    #54
                    Re: Strcpy versus Strncpy in arrays

                    On Tue, 22 Nov 2005 15:13:14 +0000, Jordan Abel wrote:[color=blue]
                    > On 2005-11-22, Mabden <mabden@sbc_glo bal.net> wrote:[color=green]
                    >> "Skarmander " <invalid@dontma ilme.com> wrote in message
                    >> news:437fb858$0 $11061$e4fe514c @news.xs4all.nl ...[color=darkred]
                    >>> Joe Wright wrote:
                    >>> > for (i = 0; i < N; ++i)
                    >>> >
                    >>> > Again I say it "increment i" and therefore '++i'.[/color]
                    >>
                    >> But in a for() loop, you want to have the idea that i will be plus-one
                    >> in the next loop, so i++ really states the idea better. In the case of
                    >> just incrementing a variable, I use "++j;" because I read it as
                    >> "increment j" as opposed to "j++;" which I read as "do nothing, then
                    >> increment j" which seems silly.[/color]
                    >
                    > Eh? That doesn't make sense.[/color]

                    Mabden probably meant "do something unnecessary" rather than "do nothing":

                    The "post" in post-increment implies that something is occurring first -
                    the storage of the original value for later use - whereas with
                    pre-increment there is no need to store a value.

                    In a void expression, there's nothing to do later with the stored value,
                    so there's no point in storing it in the first place.

                    His comment on for-loops as a special case mirrors Mark McIntyre's
                    comments which seem pretty reasonable. Since the increment occurs at the
                    end of each loop body, it's clearest for the increment operator to also
                    follow (occur at the end of) the variable.

                    --

                    Comment

                    Working...