空字符和固定字符串 [英] Null character and fixed strings

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

问题描述



直到现在我还不明白null字符如何自动添加到字符串末尾的
并且它不是字符串数组的元素。

所有书都说空字符(\0)自动添加到字符串的结尾




让我们说

char name [9] =" 123456789"

如果在循环中输入名称;


for(i = 0; i< 9; i ++)

printf("%c",name [i]);


printf("%c", name [i + 1]);

我会将名字[10]视为\0空字符吗?

Hi,
Till now I do not understand how the null character automatically added
to the end of the string and it is not an element of the string array .
All books said the null character (\0) added automatically to the end
of the string.

Let say
char name[9]="123456789"
If entered the name in a loop;

for (i=0;i<9;i++)
printf("%c",name[i]);

printf("%c",name[i+1]);
will I see name[10] as \0 Null character ?

推荐答案

eh**********@gmail.com 写道:

直到现在我还不明白null字符如何自动添加到字符串的末尾并且它不是字符串数组的元素。
所有书籍都说空字符(\0)自动添加到e中nd
字符串。

请说
char name [9] =" 123456789"
如果在循环中输入名称;


在这个赋值中,编译器从

常量nul终止字符串中复制,该字符串包含元素

" 123456789 \ 0"对于数组''name'',它有9个元素的空间
,所以它停在''9''。你得到了什么

不是一个nul终止的字符串,而是一个9字符

长数组,其中包含字符1到9.

for(i = 0; i< 9; i ++)
printf("%c",name [i]);

printf("%c",name [i + 1]) ;


这里你出错,访问

9元素数组的第10个元素。

我会看到名字[10] ]为\ 0空字符?
Hi,
Till now I do not understand how the null character automatically added
to the end of the string and it is not an element of the string array .
All books said the null character (\0) added automatically to the end
of the string.

Let say
char name[9]="123456789"
If entered the name in a loop;

In this assignment the compiler copies from the
constant nul terminated string, which has elements
"123456789\0" to the array ''name'', which has space
for 9 elements, so it stops at ''9''. What you get
is not a nul terminated string, but a nine char
long array holding the characters 1 to 9.
for (i=0;i<9;i++)
printf("%c",name[i]);

printf("%c",name[i+1]);

Here you go wrong, accessing the 10th element of a
9 element array.

will I see name[10] as \0 Null character ?




不在您的示例中(当您使用
访问名称时调用未定义的行为[9],因为名字只有9个。)


当你退出循环时i == 9,而不是10(假设这个

是一段代码而你以前做过的事情

喜欢#include< stdio.h>并宣布i)。你是

访问名称[9],这是

数组的第10个元素(从名字[0]开始)。


要将终止nul复制到名称要么声明

就像:

char name [10] =" 123456789" ;;


所以它足以容纳终止nul。或者:

char name [] =" 123456789"


编译器查看作业并命名

足够大保持整个字符串。 sizeof name

会告诉你这有多大。


-

imalone



Not in your example (you invoke undefined behaviour when
you access name[9], because name is only 9 in size).

When you exit the loop i==9, not 10 (assuming this
is a snippet of code and you''ve previously done things
like "#include <stdio.h>" and declared i). You are
accessing name[9], which is the 10th element of the
array (starting from name[0]).

To get the terminating nul copied to name either declare
it like:
char name[10] = "123456789";

So it is big enough to hold the terminating nul. Or:
char name[] = "123456789"

The compiler looks at the assignment and makes name
big enough to hold the entire string. "sizeof name"
will tell you how big that is.

--
imalone


你的意思是,必须将元素空间留在数组字符串的

结尾,以便编译器复制空字符?

如果我没有得到什么结果?

What you mean that is always a must to leave en element space in the
end of an array string to copy the null character by the compiler ?
If I did not what are the consequneces ?


eh ********** @ gmail.com 写道:

直到现在我还不明白怎么回事字符自动添加到字符串的末尾并且它不是字符串数组的元素。
所有书籍都说空字符(\0)自动添加到结尾的
字符串。

请说
char name [9] =" 123456789"
如果在循环中输入名称;

for(i = 0; i< 9; i ++)
printf("%c",name [i]);

printf("%c",name [i + 1]);

我会看到名字[10] \ 0空字符?
Hi,
Till now I do not understand how the null character automatically added
to the end of the string and it is not an element of the string array .
All books said the null character (\0) added automatically to the end
of the string.

Let say
char name[9]="123456789"
If entered the name in a loop;

for (i=0;i<9;i++)
printf("%c",name[i]);

printf("%c",name[i+1]);
will I see name[10] as \0 Null character ?




有2个原因没有。

1)数组从0开始索引而不是一个,所以你的意思是[ 9]

2)你*明确*告诉编译器只为9

字符分配空间。


如果你这样做

char name [] =" 123456789"

编译器将为10个字符分配空间,假设您需要一个

字符串(在C中*必须*由\0)终止,而不是一个不是字符串的

字符数组。


同样,如果你使用字符串除了初始化之外的其他地方

一个数组,编译器会在末尾添加\0,所以你可以这样做:


char * name = 假&

....

if(strcmp(name,fred))


没有明确添加\0自己。


如果你这样做了

char name [some_number_larger_than_9] =" 123456789"

然后元素名称[9]到数组的末尾将设置为\0,但

这是由于其他原因。
-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但它是真实的,我读了它。



No for 2 reasons.
1) Arrays are indexed from 0 not one, so you meant name[9]
2) You *explicitly* told the compiler to only allocate space for 9
chars.

If you do
char name[] = "123456789"
the compiler will allocate space for 10 chars assuming you wanted a
string (which in C *must* be terminated by a \0) rather than an array of
chars that is not a string.

Equally, if you use a string literal somewhere other than initialising
an array the compiler will add the \0 on the end, so you can do things like:

char *name = "dummy"
....
if (strcmp(name,"fred"))

without having to explicitly add the \0 yourself.

If you did
char name[some_number_larger_than_9] = "123456789"
Then elements name[9] to the end of the array would be set to \0, but
that is for a different reason.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于空字符和固定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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