sizeof字符串 [英] sizeof strings

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

问题描述

大家好,

这个可能看似有线但我不知道为什么我会混淆这个

简单的东西


问题是:


字符名称[10];


这定义了一个大小为10的字符数组,其中0-8个位置(计数) 9)

可以是字符,名称[9]应该是''\ 0''。right?


但是当我strcpy()一个字符串时将10个字符(strlen()= 10)改成

名称,它工作正常

并显示名称[9] =字符串中的有效字符。

那么名字[9]的''\ 0''字符在哪里消失了??????


为什么它没有抱怨?


谢谢

mohan gupta

hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be ''\0'' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the ''\0'' character of name[9] gone?????

and why is it not complaining???

thank you
mohan gupta

推荐答案

mohi写道:
mohi wrote:

大家好,

这个可能看似有线但我不知道为什么我混淆了这个

简单的东西

问题是:


char name [10];


这定义了一个大小为10的char数组,其中0-8个位置(计数9)

可以是字符,名称[9]应该是''\ 0''。right?
hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be ''\0'' .right?



不一定。即使在''name''中存储字符串,你也不需要

消耗所有它的元素。在''name''中存储的一个字符串

完全没问题,但它可能会浪费存储空间。

Not necessarily. Even when storing a string in ''name'' you need not
consume all it''s elements to do so. A one character string stored
in ''name'' is perfectly fine, though it may be a waste of storage.


但是当我strcpy()一个10个字符的字符串(strlen()= 10)到

名称,它工作正常

并显示名称[9] =来自的有效字符字符串。

那么名字[9]的''\ 0''字符在哪里消失了?????
but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the ''\0'' character of name[9] gone?????



strlen函数返回它的参数的字符数

指向,最多但是*不包括null字符,虽然它需要创建一个字符串,但它不是字符串的一部分。因此

你的''name''数组是一个字符太短,不能容纳一串十个

字符。它只能包含一个最多九个字符的字符串。


Strcpy会盲目地将它的第二个参数复制到它的第一个。确保第一个

参数指向的缓冲区有足够的空间来包含
$指向的字符串,这是你的责任。 b $ b秒。否则strcpy将超出目标缓冲区并且将导致
未定义的行为。

The strlen function returns the number of characters it''s argument
points to, up to, but *not* including the null character, which is not
a part of the string, though it''s needed to create a string. Therefore
your ''name'' array is one character too short to hold a string of ten
characters. It can only hold a string of maximum nine characters.

Strcpy will blindly copy it''s second argument to it''s first. It''s your
responsibility to make sure that the buffer pointed to by the first
argument has sufficient space to contain the string pointed to by the
second. Otherwise strcpy will overrun the destination buffer and
undefined behaviour will result.


为什么它不抱怨?
and why is it not complaining???



纯粹的运气。尽量不要在编程中过多依赖它。


< snip>

By pure luck. Try not to depend too much on it in programming.

<snip>


santosh写道:
santosh wrote:

mohi写道:
mohi wrote:


> char name [10];

这定义了一个大小为10的char数组,其中0-8个位置(计数9)
可以是字符,名称[9]应该是''\0' ' 。对?
>char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be ''\0'' .right?



不一定。即使在''name''中存储字符串,你也不需要

消耗所有它的元素。在''name''中存储的一个字符串

完全没问题,但它可能会浪费存储空间。


Not necessarily. Even when storing a string in ''name'' you need not
consume all it''s elements to do so. A one character string stored
in ''name'' is perfectly fine, though it may be a waste of storage.


>但是当我strcpy()一个10个字符的字符串(strlen()= 10)进入
名称时,它工作正常
并显示名称[9] =字符串中的有效字符。
所以名字[9]的''\ 0''字符在哪里消失了?????
>but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the ''\0'' character of name[9] gone?????



strlen函数返回它的参数的字符数

指向,最多但是*不包括null字符,虽然它需要创建一个字符串,但它不是字符串的一部分。


The strlen function returns the number of characters it''s argument
points to, up to, but *not* including the null character, which is not
a part of the string, though it''s needed to create a string.



空字符终止字符串,但由标准定义为

字符串的一部分:

7.1.1:

"字符串是一个连续的字符序列,以及

包括第一个空字符。


-

Thad

The null character terminates the string, but is defined by the standard as
part of the string:
7.1.1:
"A string is a contiguous sequence of characters terminated by and
including the first null character."

--
Thad


文章< e1 *************** ******************* @ n33g2000pri。 googlegroups.com>,

mohi< mo ********** @ gmail.comwrote:
In article <e1**********************************@n33g2000pri. googlegroups.com>,
mohi <mo**********@gmail.comwrote:

>这个可能看似有线但我不知道为什么我混淆了这个简单的东西
问题是:

字符名称[10];

这定义了一个大小为10的char数组,其中0-8个位置(计数9)
可以是字符,名称[9]应该是''\ 0''。。right?

但是当我strcpy()一个10个字符的字符串(strlen()= 10)进入
名称时,它工作正常
并显示名称[9] =字符串中的有效字符。
所以名字[9]的''\ 0''字符在哪里消失了?????

为什么不抱怨?
>this one might seem wired but i dont know why iam confused with this
simple stuff
the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be ''\0'' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the ''\0'' character of name[9] gone?????

and why is it not complaining???



显示[exact]代码以补充单词,否则我们只能

但是猜猜。

-

Greg Comeau / 4.3.10.1,C ++ 0xisms现在处于测试阶段!

Comeau C / C ++ ONLINE == http://www.comeaucomputing.com/tryitout

世界级编译器:令人惊叹的C ++,惊人的C99 ,Fabulous C90。

Comeau C / C ++与Dinkumware的图书馆......你试过吗?

Show the [exact] code to complement the words, otherwise we can
only but guess.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware''s Libraries... Have you tried it?


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

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