[C]简单的字符串问题 [英] [C] simple string question

查看:99
本文介绍了[C]简单的字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想定义一个恒定长度的字符串,比如4

然后在某个时间函数中,我想设置字符串为常量

值,比如说

以下是我的代码但它失败了

什么是正确的代码?

很多thx!

char string [4] = {0};


string =''a''; / *< - 失败* /

hi all,

I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a constant
value, say a
below is my code but it fails
what is the correct code?
many thx!
char string[4] = {0};

string = ''a ''; /* <-- failed */

推荐答案

Alan写道:
大家好,
然后在一个函数中,我想把字符串设置为一个
常量值,比如说
下面是我的代码但它失败了
什么是正确的代码?
很多thx!

char string [4] = {0};

string =''一个 ''; / *< - failed * /
hi all,

I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a
constant value, say a
below is my code but it fails
what is the correct code?
many thx!
char string[4] = {0};

string = ''a ''; /* <-- failed */




memcpy(" abcd",string,sizeof(string));


- Pete



memcpy("abcd", string, sizeof(string));

- Pete




" AirPete"写道:

"AirPete" wrote:
Alan写道:
大家好,

我想定义一个恒定长度的字符串,比如4
然后在函数在某个时候,我想将字符串设置为一个
常量值,比如说下面是我的代码但它失败了
什么是正确的代码?
很多thx!

char string [4] = {0};

string =''a''; / *< - 失败* /
memcpy(" abcd",string,sizeof(string));
hi all,

I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a
constant value, say a
below is my code but it fails
what is the correct code?
many thx!
char string[4] = {0};

string = ''a ''; /* <-- failed */
memcpy("abcd", string, sizeof(string));




抱歉,但它没有'当我运行程序,程序崩溃时,工作



- Pete



sorry, but it doesn''t work
when I run the program, the program crashes

- Pete



2004年2月5日星期四03:15:29 GMT,AirPete < x@x.x>写道:
On Thu, 05 Feb 2004 03:15:29 GMT, "AirPete" <x@x.x> wrote:
Alan写道:
大家好,

我想定义一个恒定长度的字符串,比如4
然后在某个时间的某个函数中,我想将字符串设置为
常量值,比如说下面是我的代码但是它失败了
什么是正确的代码?
很多thx!

char string [4] = {0};

string =''a''; / *< - failed * /
hi all,

I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a
constant value, say a
below is my code but it fails
what is the correct code?
many thx!
char string[4] = {0};

string = ''a ''; /* <-- failed */



memcpy(" abcd",string,sizeof(string));

- Pete



memcpy("abcd", string, sizeof(string));

- Pete




memcpy看起来有点矫枉过正,加上用法是:

memcpy(目的地,来源,no_of_bytes);

顶部其中,上面的memcpy调用将占用char数组中每个现有的

位置,不会留下终止NUL,这可能会导致严重的肮脏。

肯定会导致严重的肮脏。 >

要初始化字符串,相当于IMO更清晰的方式

它将是:

char string [4] =" ;英寸; //仍然全部被淘汰


重点是自我记录意图;你代表一个

空的字符串而不是一堆数字字节。如果你真的想要强调第一个字节是NUL的事实。 (

字符种类),你可以改为说:

char string [4] = {''\ 0''}; //但请不要。


要更改字符串的值,普通的旧strcpy有效:


strcpy(string, a);


如果你担心可能覆盖阵列的四个可用的

字节(它不会发生在上面的调用,但可能是

,如果你不确定字符串的长度是什么,那么从b中复制的是b $ b,请考虑strncpy:


strncpy(字符串,some_source_ptr,4);


但是,这个_still_有可能留下没有
$ b的字符串$ b终止null。你只需要小心。


BTW,整个代码中散布的4'都是糟糕的风格。在C中,使用

#define将长度设置在某个顶部...


祝你好运,

-leor


Leor Zolman

BD软件
le ** @ bdsoft.com
www.bdsoft.com - 关于 - C / C ++,Java,Perl& C中的现场培训Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html



memcpy seems overkill for this, plus the usage is:
memcpy(destination, source, no_of_bytes);
On top of that, the memcpy call above would use up every existing
position in the char array, leaving no terminating NUL, which could
definitely lead to serious nastiness.

To initialize the string, an equivalent but in IMO clearer way to do
it would be:
char string[4] = ""; // still all nulled out

The point is to self-document the intention; you''re representing an
empty "string" as opposed to "a bunch of numeric bytes". If you really
want to underscore the fact that the first byte is a "NUL" (the
character variety), you can instead say:
char string[4] = {''\0''}; // but please don''t.

To change the value of the string, plain old strcpy works:

strcpy(string, "a");

If you''re concerned about possibly overwriting the four available
bytes of the array (it wouldn''t happen with the call above, but might
if you don''t know for sure what the length of the string you''re
copying from is), consider strncpy:

strncpy(string, some_source_ptr, 4);

However, this _still_ has the potential of leaving string without a
terminating null. You just have to be careful.

BTW, the 4''s sprinkled throughout the code are bad style. In C, use a
#define to set that length up at the top somewhere...

Good luck,
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


这篇关于[C]简单的字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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