Problem with strcat, strcpy,sprintf

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

    #16
    Re: Problem with strcat, strcpy,sprintf

    Arctic Fidelity wrote:[color=blue]
    >
    > On Fri, 28 Oct 2005 18:34:47 -0400,
    > Keith Thompson <kst-u@mib.org> wrote:[/color]
    [color=blue][color=green][color=darkred]
    > >> void main(void)[/color]
    > >
    > > No, no, no, no, no.
    > >
    > > main() returns int, not void.[/color]
    >
    > :-O I never knew...Wah?? Gah! Ouch.
    > I'll keep that in definite mind next time.[/color]

    The rules are that main returns int,
    but implementations may accept alternate forms of main.

    Since the alternate forms are not standard,
    and this is a newsgroup about C and not about *your* compiler,
    the alternate forms are off topic here.

    --
    pete

    Comment

    • Jordan Abel

      #17
      Re: Problem with strcat, strcpy,sprintf

      On 2005-10-29, pete <pfiland@mindsp ring.com> wrote:[color=blue]
      > Arctic Fidelity wrote:[color=green]
      >>
      >> On Fri, 28 Oct 2005 18:34:47 -0400,
      >> Keith Thompson <kst-u@mib.org> wrote:[/color]
      >[color=green][color=darkred]
      >> >> void main(void)
      >> >
      >> > No, no, no, no, no.
      >> >
      >> > main() returns int, not void.[/color]
      >>
      >> :-O I never knew...Wah?? Gah! Ouch.
      >> I'll keep that in definite mind next time.[/color]
      >
      > The rules are that main returns int, but implementations may
      > accept alternate forms of main.
      >
      > Since the alternate forms are not standard, and this is a
      > newsgroup about C and not about *your* compiler, the alternate
      > forms are off topic here.[/color]

      Furthermore, very few implementations _actually_ accept void main.
      It's just that a few of the most common ones happen to not do
      anything worse than having some arbitrary number as the exit status

      Comment

      • Mark McIntyre

        #18
        Re: Problem with strcat, strcpy,sprintf

        On Fri, 28 Oct 2005 23:38:31 GMT, in comp.lang.c , Keith Thompson
        <kst-u@mib.org> wrote:
        [color=blue]
        >Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
        >> On 28 Oct 2005 14:09:43 -0700, in comp.lang.c ,
        >> diego.arias.vel @gmail.com wrote:[/color]
        >[...][color=green][color=darkred]
        >>>void copy(char *filename)
        >>>{
        >>> char *log_file;
        >>> log_file=filena me;[/color]
        >>
        >> This points log_file to the same place as filename.
        >>
        >> Remember that in C, = is not the copy operator, its the assignment
        >> operator. For pointer types, this sets the pointers to point to the
        >> same place. It does /not/ copy the contents.[/color]
        >[...]
        >
        >I don't think I'd phrase it that way.[/color]

        Yeah, yours is perhaps more correct phrasing, but IMO that would have
        totally confused anyone who thought of "string" as an actual type.
        Since this is a very common newby view, I deliberately chose different
        wording.

        [color=blue]
        >Another way to put it is that "=" does a shallow copy, not a deep
        >copy; it copies only the value itself,[/color]

        As a newby I'd expect this to copy the string...

        --
        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-Uncensored-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

          #19
          Re: Problem with strcat, strcpy,sprintf

          On 28 Oct 2005 16:55:25 -0700, in comp.lang.c , "nelu"
          <tandauioan@gma il.com> wrote:
          [color=blue]
          >I've seen this in a lot of places and I've been wandering if it's smart
          >to write code like this:
          >
          >log_file=mallo c(STRGSIZE)[/color]

          This is the right way.
          [color=blue]
          >instead of:
          >
          >log_file=(ch ar *)malloc(STRGSI ZE*sizeof(char) )[/color]

          In this version,
          a) the cast is not needed and can conceal a serious error
          b) sizeof(char) is by definition 1, so its not needed.
          [color=blue]
          >because it is both a portability problem[/color]

          only between C and C++
          [color=blue]
          >and a lot of students don't
          >get the idea and do the exact same thing for types other than char?[/color]

          in that case use the form
          log_file = malloc (STRGSIZE * sizeof (*log_file));
          and no matter what log_file is defined as, you're clear.
          --
          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-Uncensored-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

            #20
            Re: Problem with strcat, strcpy,sprintf

            Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
            > On Fri, 28 Oct 2005 23:38:31 GMT, in comp.lang.c , Keith Thompson
            > <kst-u@mib.org> wrote:[color=green]
            >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
            >>> On 28 Oct 2005 14:09:43 -0700, in comp.lang.c ,
            >>> diego.arias.vel @gmail.com wrote:[/color]
            >>[...][color=darkred]
            >>>>void copy(char *filename)
            >>>>{
            >>>> char *log_file;
            >>>> log_file=filena me;
            >>>
            >>> This points log_file to the same place as filename.
            >>>
            >>> Remember that in C, = is not the copy operator, its the assignment
            >>> operator. For pointer types, this sets the pointers to point to the
            >>> same place. It does /not/ copy the contents.[/color]
            >>[...]
            >>
            >>I don't think I'd phrase it that way.[/color]
            >
            > Yeah, yours is perhaps more correct phrasing, but IMO that would have
            > totally confused anyone who thought of "string" as an actual type.
            > Since this is a very common newby view, I deliberately chose different
            > wording.[/color]

            I think that just reinforces any confusion. Anyone who thinks that
            "string" is an actual type should be told that it isn't.
            [color=blue][color=green]
            >>Another way to put it is that "=" does a shallow copy, not a deep
            >>copy; it copies only the value itself,[/color]
            >
            > As a newby I'd expect this to copy the string...[/color]

            Not if you understand what a "string" is. (You're not restricted to
            explaining just one thing at a time.)

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

              #21
              Re: Problem with strcat, strcpy,sprintf

              On Sat, 29 Oct 2005 22:14:45 GMT, in comp.lang.c , Keith Thompson
              <kst-u@mib.org> wrote:
              [color=blue]
              >Not if you understand what a "string" is. (You're not restricted to
              >explaining just one thing at a time.)[/color]

              I've noticed when teaching undergrads (and new staff) that the
              teach-several-things-at-once approach is fraught with peril... :-)

              --
              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-Uncensored-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

                #22
                Re: Problem with strcat, strcpy,sprintf

                Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                > On Sat, 29 Oct 2005 22:14:45 GMT, in comp.lang.c , Keith Thompson
                > <kst-u@mib.org> wrote:
                >[color=green]
                >>Not if you understand what a "string" is. (You're not restricted to
                >>explaining just one thing at a time.)[/color]
                >
                > I've noticed when teaching undergrads (and new staff) that the
                > teach-several-things-at-once approach is fraught with peril... :-)[/color]

                Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
                their-misconceptions-are-correct approach fraught with less peril?

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

                  #23
                  Re: Problem with strcat, strcpy,sprintf

                  On Sun, 30 Oct 2005 19:59:43 GMT, in comp.lang.c , Keith Thompson
                  <kst-u@mib.org> wrote:
                  [color=blue]
                  >Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
                  >> On Sat, 29 Oct 2005 22:14:45 GMT, in comp.lang.c , Keith Thompson
                  >> <kst-u@mib.org> wrote:
                  >>[color=darkred]
                  >>>Not if you understand what a "string" is. (You're not restricted to
                  >>>explaining just one thing at a time.)[/color]
                  >>
                  >> I've noticed when teaching undergrads (and new staff) that the
                  >> teach-several-things-at-once approach is fraught with peril... :-)[/color]
                  >
                  >Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
                  >their-misconceptions-are-correct approach fraught with less peril?[/color]

                  Pejorative, rigged question and therefore not worthy of an answer. :-)

                  Ignoring your question, I'd say its much less fraught with peril to
                  start with fundamentals so that by the time the students get to such
                  complexities as the = operator, they already don't think strings are
                  PODs.
                  --
                  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-Uncensored-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

                    #24
                    Re: Problem with strcat, strcpy,sprintf

                    Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                    > On Sun, 30 Oct 2005 19:59:43 GMT, in comp.lang.c , Keith Thompson
                    > <kst-u@mib.org> wrote:
                    >[color=green]
                    >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
                    >>> On Sat, 29 Oct 2005 22:14:45 GMT, in comp.lang.c , Keith Thompson
                    >>> <kst-u@mib.org> wrote:
                    >>>
                    >>>>Not if you understand what a "string" is. (You're not restricted to
                    >>>>explainin g just one thing at a time.)
                    >>>
                    >>> I've noticed when teaching undergrads (and new staff) that the
                    >>> teach-several-things-at-once approach is fraught with peril... :-)[/color]
                    >>
                    >>Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
                    >>their-misconceptions-are-correct approach fraught with less peril?[/color]
                    >
                    > Pejorative, rigged question and therefore not worthy of an answer. :-)[/color]

                    I see the smiley, but the question was based directly on what you
                    wrote upthread. It may have been a bit pejorative, but it was not
                    rigged, at least not deliberately.

                    Mark:
                    ] >> Remember that in C, = is not the copy operator, its the assignment
                    ] >> operator. For pointer types, this sets the pointers to point to the
                    ] >> same place. It does /not/ copy the contents.
                    ] >[...]
                    Keith:
                    ] >I don't think I'd phrase it that way.
                    Mark:
                    ] Yeah, yours is perhaps more correct phrasing, but IMO that would have
                    ] totally confused anyone who thought of "string" as an actual type.
                    ] Since this is a very common newby view, I deliberately chose different
                    ] wording.

                    It certainly appeared to me that you were assuming certain
                    misconceptions about what strings are and deliberately not correcting
                    them for the sake of an explanation of what assignment does. For
                    example, I found your use of the word "contents" misleading; the
                    content of a pointer is an address, not the thing it points to.
                    Saying that "=" is not the copy operator" is not a simplification;
                    it's just incorrect. It might be a convenient fiction that can help
                    someone understand that in
                    char *a = "hello";
                    char *b;
                    b = a;
                    the assignment doesn't copy the string -- but it will inevitably lead
                    to confusion later on.

                    There are times when it's appropriate to give not-quite-correct
                    explanations early on, to be refined later. I don't think this is one
                    of those cases. The subtle relationship between arrays and pointers
                    is so central to the way C works that understanding it is an absolute
                    prerequesite to having a real understanding of the language.
                    [color=blue]
                    > Ignoring your question, I'd say its much less fraught with peril to
                    > start with fundamentals so that by the time the students get to such
                    > complexities as the = operator, they already don't think strings are
                    > PODs.[/color]

                    (POD being Plain Old Data, I presume; I think that's mostly a C++
                    term, so some here might not be familiar with it.)

                    I agree with you, but that's not the situation here. The question is
                    how to explain pointer assignment to someone who *already*
                    misunderstands C strings. You can give a quick and misleading
                    explanation that lets the newbie *think* he understands how a
                    particular program works, or you can give a longer and more correct
                    explanation, or at least a pointer to one (such as, "read section 6 of
                    the C FAQ").

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

                    • Greg Comeau

                      #25
                      Re: Problem with strcat, strcpy,sprintf

                      In article <ln1x22u66o.fsf @nuthaus.mib.or g>,
                      Keith Thompson <kst-u@mib.org> wrote:[color=blue]
                      >Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
                      >> On Sat, 29 Oct 2005 22:14:45 GMT, in comp.lang.c , Keith Thompson
                      >> <kst-u@mib.org> wrote:
                      >>[color=darkred]
                      >>>Not if you understand what a "string" is. (You're not restricted to
                      >>>explaining just one thing at a time.)[/color]
                      >>
                      >> I've noticed when teaching undergrads (and new staff) that the
                      >> teach-several-things-at-once approach is fraught with peril... :-)[/color]
                      >
                      >Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
                      >their-misconceptions-are-correct approach fraught with less peril?[/color]

                      I find teaching to misconceptions misconcepted and so always teach
                      the truth. The "small lies" (and big ones) just never make any sense
                      to me when I hear others saying that they teach that way.
                      --
                      Greg Comeau / Celebrating 20 years of Comeauity!
                      Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
                      World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                      Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                      Comment

                      Working...