By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

using strcpy to copy from a char

P: n/a
#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?

Mar 24 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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.

Mar 24 '07 #2

P: n/a

"Alok Kumar" <al********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.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.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 24 '07 #3

P: n/a
On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gmail.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.
Mar 24 '07 #4

P: n/a
On Mar 24, 7:03 pm, "Alok Kumar" <alok.ku...@gmail.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.

Mar 25 '07 #5

P: n/a

<kl**********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gmail.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.
Mar 25 '07 #6

P: n/a
On 25 maalis, 02:33, "Barry" <bar...@nullhighstream.netwrote:
<klaushuot...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...


On 24 maalis, 21:03, "Alok Kumar" <alok.ku...@gmail.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.

Mar 25 '07 #7

P: n/a
On Mar 24, 12:03 pm, "Alok Kumar" <alok.ku...@gmail.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
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 25 '07 #8

P: n/a
we******@gmail.com wrote:
On Mar 24, 12:03 pm, "Alok Kumar" <alok.ku...@gmail.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

Mar 25 '07 #9

P: n/a
"Malcolm McLean" <re*******@btinternet.comwrites:
"Alok Kumar" <al********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.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_Keith) ks***@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"
Mar 25 '07 #10

P: n/a

"CBFalconer" <cb********@yahoo.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.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 25 '07 #11

P: n/a
>In article <46***************@yahoo.comCBFalconer <cb********@yahoo.com>

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

In article <8M******************************@bt.com>,
Malcolm McLean <re*******@btinternet.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.
Mar 25 '07 #12

P: n/a
"Chris Torek" <no****@torek.netwrote in message
In article <46***************@yahoo.comCBFalconer
<cb********@yahoo.com>

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

In article <8M******************************@bt.com>,
Malcolm McLean <re*******@btinternet.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.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 25 '07 #13

P: n/a

"Chris Torek" <no****@torek.netha scritto nel messaggio
news:eu*********@news1.newsguy.com...
Or, in other words, mathematicians are odd. (Except when even,
complex, or irrational.)
What if they are fractional?
Mar 25 '07 #14

P: n/a
In article <eu*********@news1.newsguy.com>,
Chris Torek <no****@torek.netwrote:
>>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.
Mar 25 '07 #15

P: n/a
Army1987 wrote:
"Chris Torek" <no****@torek.netha 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

Mar 25 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.