using strcpy to copy from a char

Discussion in 'C Programming' started by Alok Kumar, Mar 24, 2007.

  1. Alok  Kumar

    Alok Kumar Guest

    #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?
    Alok Kumar, Mar 24, 2007
    #1
    1. Advertising

  2. 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.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Mar 24, 2007
    #2
    1. Advertising

  3. "Alok Kumar" <> wrote in message
    news:...
    > #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.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 24, 2007
    #3
  4. Alok  Kumar

    Guest

    On 24 maalis, 21:03, "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?


    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.
    , Mar 24, 2007
    #4
  5. On Mar 24, 7:03 pm, "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?


    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.
    christian.bau, Mar 25, 2007
    #5
  6. Alok  Kumar

    Barry Guest

    <> wrote in message
    news:...
    > On 24 maalis, 21:03, "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?

    >
    > 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.
    Barry, Mar 25, 2007
    #6
  7. Alok  Kumar

    Guest

    On 25 maalis, 02:33, "Barry" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > On 24 maalis, 21:03, "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?

    >
    > > 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.
    , Mar 25, 2007
    #7
  8. Alok  Kumar

    Guest

    On Mar 24, 12:03 pm, "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?


    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
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
    , Mar 25, 2007
    #8
  9. Alok  Kumar

    CBFalconer Guest

    wrote:
    > On Mar 24, 12:03 pm, "Alok Kumar" <> wrote:
    >
    >> #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
    CBFalconer, Mar 25, 2007
    #9
  10. "Malcolm McLean" <> writes:
    > "Alok Kumar" <> wrote in message
    > news:...
    >> #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_Keith) <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"
    Keith Thompson, Mar 25, 2007
    #10
  11. "CBFalconer" <> wrote 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.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 25, 2007
    #11
  12. Alok  Kumar

    Chris Torek Guest

    >In article <> CBFalconer <>

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

    In article <>,
    Malcolm McLean <> wrote:
    >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.
    Chris Torek, Mar 25, 2007
    #12
  13. "Chris Torek" <> wrote in message
    > >In article <> CBFalconer
    > ><>

    >
    > ... pointed out that a single "char" can hold a C string, as long as
    > the string is the empty string.
    >
    > In article <>,
    > Malcolm McLean <> wrote:
    >>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.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 25, 2007
    #13
  14. Alok  Kumar

    Army1987 Guest

    "Chris Torek" <> ha scritto nel messaggio
    news:...
    > Or, in other words, mathematicians are odd. (Except when even,
    > complex, or irrational.)

    What if they are fractional?
    Army1987, Mar 25, 2007
    #14
  15. In article <>,
    Chris Torek <> wrote:

    >>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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Mar 25, 2007
    #15
  16. Alok  Kumar

    CBFalconer Guest

    Army1987 wrote:
    > "Chris Torek" <> ha scritto nel messaggio
    >
    >> Or, in other words, mathematicians are odd. (Except when even,
    >> complex, or irrational.)

    >
    > What if they are fractional?


    Or unhinged.

    --
    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
    CBFalconer, Mar 25, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RonHiler
    Replies:
    8
    Views:
    514
    John Harrison
    Oct 19, 2004
  2. Replies:
    18
    Views:
    5,368
    Andre Kostur
    Jun 8, 2005
  3. Alex
    Replies:
    2
    Views:
    1,223
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,042
    Ian Collins
    May 9, 2006
  5. kaizen
    Replies:
    3
    Views:
    4,684
    Jim Langston
    Jan 21, 2006
Loading...

Share This Page