473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Mar 24 '07 #1
15 6620
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

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

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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
24
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
4
by: ohaqqi | last post by:
Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the...
9
by: chikkubhai | last post by:
Why is the result different for the following set of two code snippets Code without using this pointer #include <string> #include <iostream> using namespace std; struct X { private:
16
by: mdh | last post by:
I have a question about the library function strcpy. In K&R on page 109, strcpy is used to copy a line ( line ) to a pointer (char *), which is first allocated space by K&Rs "alloc" function. In...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.