使用strcpy从char复制 [英] using strcpy to copy from a char

查看:97
本文介绍了使用strcpy从char复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< string.h>

void myfn()

{

char a =''A'';

char b [2];

strcpy(b,& a);

}


strcpy之后我会在b [0]和b [1]中的''\ 0''中得到'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?

推荐答案

Alok Kumar写道:
Alok Kumar wrote:

#include< string.h>

void myfn()

{

char a =''A'';

char b [2];

strcpy(b,& ; a);

}


我总是在b [0]和''\ 0''中得到'A'' 1] strcpy之后?
#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?



不,你不能安全地假设。 strcpy将尝试读取*(& a + 1)

并将其与''\0''进行比较,但是a之后的字节不一定是你的

阅读,即使它是,它也不一定是0.

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.




" Alok Kumar < al ******** @ gmail.com在留言中写道

新闻:11 ********************* @ o5g2000hsb.googlegrou ps.com ...

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

}


我总是会在b [0]和'[\\ 0''中的b [1]中得到'A''在strcpy之后?
#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?



编号strcpy将复制值,直到它达到零字节。在第一次运行时,

很可能是b [0],并且该函数看起来像你想要的那样工作。然而,最终下面的字节可能不会为零,并且

功能将通过内存来消耗它不会带来不可预测的

后果。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

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


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


}


我会在b [0]和''\\'中得到''A'' strcpy之后b [1]中的0''?
#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?



不太可能。 A不会被NULL字节终止。它只是一个字母

常数。


如果你愿意这样:


# include< string.h>

void myfn()

{

char a =" A";

char b [2];

strcpy(b,& a);


}


你会得到A。总是在b数组中。

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.


这篇关于使用strcpy从char复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆