using strcpy to copy from a char

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

    using strcpy to copy from a char

    #include <string.h>
    void myfn()
    {
    char a = 'A';
    char b[2];
    strcpy(b, &a);
    }

    Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?

  • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

    #2
    Re: using strcpy to copy from a char

    Alok Kumar wrote:
    #include <string.h>
    void myfn()
    {
    char a = 'A';
    char b[2];
    strcpy(b, &a);
    }
    >
    Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
    No, you can't safely assume that. strcpy will try to read *(&a + 1)
    and compare it to '\0', but the byte after a isn't necessarily yours
    to read, and even if it is, it's not sure to be 0.

    Comment

    • Malcolm McLean

      #3
      Re: using strcpy to copy from a char


      "Alok Kumar" <alok.kumar@gma il.comwrote in message
      news:1174763021 .983319.55720@o 5g2000hsb.googl egroups.com...
      #include <string.h>
      void myfn()
      {
      char a = 'A';
      char b[2];
      strcpy(b, &a);
      }
      >
      Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
      >
      No. strcpy will copy values until it hits a zero byte. On the first run that
      will quite likely be the b[0], and the function will appear to work as you
      want. However eventually the following byte may not be zero, and the
      fucntion will plough through memory it doesn't own with unpredictable
      consequences.
      --
      Free games and programming goodies.


      Comment

      • klaushuotari@gmail.com

        #4
        Re: using strcpy to copy from a char

        On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gma il.comwrote:
        #include <string.h>
        void myfn()
        {
        char a = 'A';
        char b[2];
        strcpy(b, &a);
        >
        }
        >
        Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
        Not likely. 'A' is not terminated by NULL byte. It's just a char
        constant.

        If you would put it like this:

        #include <string.h>
        void myfn()
        {
        char a = "A";
        char b[2];
        strcpy(b, &a);

        }

        You would get "A" always in b array.


        Comment

        • christian.bau

          #5
          Re: using strcpy to copy from a char

          On Mar 24, 7:03 pm, "Alok Kumar" <alok.ku...@gma il.comwrote:
          #include <string.h>
          void myfn()
          {
          char a = 'A';
          char b[2];
          strcpy(b, &a);
          >
          }
          >
          Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
          Each and every time while you are developing your program.
          As soon as the program is used seriously and failure would cost you
          money, your program will crash.

          Comment

          • Barry

            #6
            Re: using strcpy to copy from a char


            <klaushuotari@g mail.comwrote in message
            news:1174777042 .779580.207460@ o5g2000hsb.goog legroups.com...
            On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gma il.comwrote:
            >#include <string.h>
            >void myfn()
            >{
            > char a = 'A';
            > char b[2];
            > strcpy(b, &a);
            >>
            >}
            >>
            >Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
            >
            Not likely. 'A' is not terminated by NULL byte. It's just a char
            constant.
            >
            If you would put it like this:
            >
            #include <string.h>
            void myfn()
            {
            char a = "A";
            char b[2];
            strcpy(b, &a);
            >
            }
            >
            You would get "A" always in b array.
            >
            >
            You need a compiler.


            Comment

            • klaushuotari@gmail.com

              #7
              Re: using strcpy to copy from a char

              On 25 maalis, 02:33, "Barry" <bar...@nullhig hstream.netwrot e:
              <klaushuot...@g mail.comwrote in message
              >
              news:1174777042 .779580.207460@ o5g2000hsb.goog legroups.com...
              >
              >
              >
              >
              >
              On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gma il.comwrote:
              #include <string.h>
              void myfn()
              {
              char a = 'A';
              char b[2];
              strcpy(b, &a);
              >
              }
              >
              Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
              >
              Not likely. 'A' is not terminated by NULL byte. It's just a char
              constant.
              >
              If you would put it like this:
              >
              #include <string.h>
              void myfn()
              {
              char a = "A";
              char b[2];
              strcpy(b, &a);
              >
              }
              >
              You would get "A" always in b array.
              >
              You need a compiler.- Piilota siteerattu teksti -
              >
              - Näytä siteerattu teksti -
              I think you're right.

              Comment

              • websnarf@gmail.com

                #8
                Re: using strcpy to copy from a char

                On Mar 24, 12:03 pm, "Alok Kumar" <alok.ku...@gma il.comwrote:
                #include <string.h>
                void myfn() {
                char a = 'A';
                char b[2];
                strcpy(b, &a);
                }
                >
                Would I always get 'A' in b[0] and '\0' in b[1] after the
                strcpy?
                Welcome to the lack of type safety that is the C language.

                Although you made the variable a into a char *, you did not make it a
                string. So when you pass its address as the second parameter to
                strcpy, you are actually invoking "undefined behavior". Taking the
                address of a char does not make it a "string" in the sense of the C
                language. Specifically a which is 'A' is not necessarily followed by
                a '\0'. (To be a string in C, you have to be a sequence of characters
                terminated by a '\0'.)

                The language lets you compile and even try to run this, but it doesn't
                make any sense. The reason is that strings in C are defined
                semantically, not syntactically. (And there is no type checking for
                semantics in C.)

                --
                Paul Hsieh
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



                Comment

                • CBFalconer

                  #9
                  Re: using strcpy to copy from a char

                  websnarf@gmail. com wrote:
                  On Mar 24, 12:03 pm, "Alok Kumar" <alok.ku...@gma il.comwrote:
                  >
                  >#include <string.h>
                  >void myfn() {
                  > char a = 'A';
                  > char b[2];
                  > strcpy(b, &a);
                  >}
                  >>
                  .... snip ...
                  >
                  Although you made the variable a into a char *, you did not make
                  it a string. So when you pass its address as the second parameter
                  to strcpy, you are actually invoking "undefined behavior". Taking
                  the address of a char does not make it a "string" in the sense of
                  the C language. Specifically a which is 'A' is not necessarily
                  followed by a '\0'. (To be a string in C, you have to be a
                  sequence of characters terminated by a '\0'.)
                  Pedantically wrong. a is perfectly capable of holding a string, as
                  long as that string does not exceed zero length. As initialized, a
                  is not a string. Char arrays can hold strings. An empty string is
                  not an empty char array.

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>



                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Keith Thompson

                    #10
                    Re: using strcpy to copy from a char

                    "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                    "Alok Kumar" <alok.kumar@gma il.comwrote in message
                    news:1174763021 .983319.55720@o 5g2000hsb.googl egroups.com...
                    >#include <string.h>
                    >void myfn()
                    >{
                    > char a = 'A';
                    > char b[2];
                    > strcpy(b, &a);
                    >}
                    >>
                    >Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
                    >>
                    No. strcpy will copy values until it hits a zero byte. On the first
                    run that will quite likely be the b[0], and the function will appear
                    to work as you want. However eventually the following byte may not be
                    zero, and the fucntion will plough through memory it doesn't own with
                    unpredictable consequences.
                    It's much worse than that. There's no reason to assume that b[0] is
                    initially equal to '\0', or that b immediately follows a in memory.

                    I just tried the above function with a few printf() statements added.
                    The initial value of b[0] was 100, and "a" *followed* b in memory,
                    after a 1-byte gap. (The strcpy() call caused a segmentation fault.)

                    --
                    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."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Malcolm McLean

                      #11
                      Re: using strcpy to copy from a char


                      "CBFalconer " <cbfalconer@yah oo.comwrote in message
                      Pedantically wrong. a is perfectly capable of holding a string, as
                      long as that string does not exceed zero length. As initialized, a
                      is not a string. Char arrays can hold strings. An empty string is
                      not an empty char array.
                      >
                      Is one sausage a string of sausages?

                      --
                      Free games and programming goodies.


                      Comment

                      • Chris Torek

                        #12
                        Re: using strcpy to copy from a char

                        >In article <4605D480.BA7DF 205@yahoo.comCB Falconer <cbfalconer@yah oo.com>

                        .... pointed out that a single "char" can hold a C string, as long as
                        the string is the empty string.

                        In article <8MSdnbu8MIIRuZ vbnZ2dnUVZ8vedn Z2d@bt.com>,
                        Malcolm McLean <regniztar@btin ternet.comwrote :
                        >Is one sausage a string of sausages?
                        Yes. In fact, "no sausages" is also a string of sausages. :-)

                        (Both are "degenerate cases", and if this were mathematics, one
                        would just specify "n >= 2" or "excluding degenerate cases" or
                        whatever.)

                        Or, in other words, mathematicians are odd. (Except when even,
                        complex, or irrational.) (I started college in a "math for people
                        who want to get a PhD in math and become a Math Professor at a
                        university" course, but ended up doing CS instead: it was easier,
                        more fun, and way more profitable. :-) )
                        --
                        In-Real-Life: Chris Torek, Wind River Systems
                        Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                        email: forget about it http://web.torek.net/torek/index.html
                        Reading email is like searching for food in the garbage, thanks to spammers.

                        Comment

                        • Malcolm McLean

                          #13
                          Re: using strcpy to copy from a char

                          "Chris Torek" <nospam@torek.n etwrote in message
                          In article <4605D480.BA7DF 205@yahoo.comCB Falconer
                          <cbfalconer@yah oo.com>
                          >
                          ... pointed out that a single "char" can hold a C string, as long as
                          the string is the empty string.
                          >
                          In article <8MSdnbu8MIIRuZ vbnZ2dnUVZ8vedn Z2d@bt.com>,
                          Malcolm McLean <regniztar@btin ternet.comwrote :
                          >>Is one sausage a string of sausages?
                          >
                          Yes. In fact, "no sausages" is also a string of sausages. :-)
                          >
                          (Both are "degenerate cases", and if this were mathematics, one
                          would just specify "n >= 2" or "excluding degenerate cases" or
                          whatever.)
                          >
                          Or, in other words, mathematicians are odd. (Except when even,
                          complex, or irrational.) (I started college in a "math for people
                          who want to get a PhD in math and become a Math Professor at a
                          university" course, but ended up doing CS instead: it was easier,
                          more fun, and way more profitable. :-) )
                          >
                          I am plannig to design a new language. It is going to be largely graphical
                          rather than in traditional text source.

                          To give you a bit of backgound, my current idea is to have two atomic data
                          types, a real and a "symbols". Symbols will be 64-bit values consisting of
                          a type and an index. Type 1 is the natural integers, of course, whilst type
                          0 is the list of symbol types themselves.

                          One thing I am try to work out is how to best represent a "wire" carrying no
                          signal rather than a wire carrying a signal of zero. On option is to se the
                          first wire to nan, the other is not to distinguish between zero and no
                          signal. Ditto with symbols, 0-0 can be the null symbol, but do I need
                          another null to represent no nothings?

                          --
                          Free games and programming goodies.




                          Comment

                          • Army1987

                            #14
                            Re: using strcpy to copy from a char


                            "Chris Torek" <nospam@torek.n etha scritto nel messaggio
                            news:eu5cfu01db f@news1.newsguy .com...
                            Or, in other words, mathematicians are odd. (Except when even,
                            complex, or irrational.)
                            What if they are fractional?


                            Comment

                            • Richard Tobin

                              #15
                              Re: using strcpy to copy from a char

                              In article <eu5cfu01dbf@ne ws1.newsguy.com >,
                              Chris Torek <nospam@torek.n etwrote:
                              >>Is one sausage a string of sausages?
                              >
                              >Yes. In fact, "no sausages" is also a string of sausages. :-)
                              >
                              >(Both are "degenerate cases", and if this were mathematics, one
                              >would just specify "n >= 2" or "excluding degenerate cases" or
                              >whatever.)
                              Is this were mathematics, we would distinguish between sausage and
                              {sausage}. Or, since we want things ordered, between sausage and
                              (sausage). C has an automatic conversion between (spam eggs sausage ...)
                              and &spam, so you can use &sausage in the same contexts as (sausage).
                              >Or, in other words, mathematicians are odd.
                              You should hear what mathematicians say about computer scientists.

                              -- Richard
                              --
                              "Considerat ion shall be given to the need for as many as 32 characters
                              in some alphabets" - X3.4, 1963.

                              Comment

                              Working...