字符串和空字符 [英] String and null character

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

问题描述

关于字符串的一些问题:这是我的测试代码的提取部分:


/ ** ** /


char * arry;

arry = malloc(30 * sizeof(arry));


arry [0] =''B'';

arry [1] =''S'';

arry [2] =''c'';


/ ** ** /


问题是:


1.现在,变量arry立即包含空字符,因为

malloc?


2.或者我应该在arry [3]追加'0'? (实际上我接下来做的是什么,但是在没有''\0''的情况下测试编译器时是
,它有效(??)


因为在这两种情况下(无论我是否附加''\0''),如果我使用

函数atoi或任何其他字符串函数,编译器

不会发出警告或错误信息,发生了什么?


提前致谢

解决方案

" Chapman"< ; ch ** @ hotmail.com>写道:

关于字符串的一些问题:这是我测试代码的提取部分:

/ ** * * /

char * arry;
arry = malloc(30 * sizeof(arry));
您分配内存以容纳30个指向字符的指针,这显然是

不是你的意图;做这个:


arry = malloc(30 * sizeof * arry);

arry [0] = ''B'';
arry [1] =''S'';
arry [2] =''c'';

/ ** ** /

任务离子是:

1.现在,变量arry是否包含空字符,因为malloc是什么?
No.

2。或者我应该在arry [3]追加0?
如果你打算用它作为一个字符串:是的。

(实际上我接下来做了什么,但是在没有''\0''的情况下测试编译器时是
,它工作(??)
这只是(坏)运气;几乎*任何*都可能发生。

因为在这两种情况下(我是否附加''\0'''或不),如果我使用
函数atoi或任何其他字符串函数,编译器
不会发出警告或错误消息,发生了什么?



编译器无法检查在
运行时分配和更改的内容。你必须确保你的字符串是''\0'' - 终止。


问候


Irrwahn

-

闭上眼睛,按三次逃生。




" Chapman"< ch ** @ hotmail.com>写在留言中

news:il ****** ************** @ news-server.bigpond.net.au ...

一些问题关于字符串的问题:这是我测试代码的提取部分:

/ ** ** /

char * arry;
arry = malloc(30 *的sizeof(ARRY));


如果你想分配一个缓冲区来保存字符串,你应该写一下


arry = malloc(30); / *这种方式是正确的。 sizeof char被定义为1

字节,你不需要在这上面使用sizeof * /





arry = malloc(30 * sizeof * arry)/ *你应该在'* arry'上使用sizeof (其中
是单个字符),而不是char *(这是一个指针。我认为这不是你想要的b / b,对吧?)。另外,不要使用sizeof(变量)。 ,使用sizeof

变量或sizeof(可变类型)或sizeof(可变类型)。代替。 * /


arry [0] =''B'';
arry [1] =''S'';
arry [2] =' 'c'';

/ ** ** /

问题是:

1.现在变量arry包含空字符由于malloc,立即



编号如果您想在分配后将所有元素填充为零,您可以

考虑calloc

2.或者我应该在arry [3]追加0?


是的。


(实际上我接下来做了什么,但在没有''\0''的情况下测试编译器时,它工作(??)


不要相信这一点,你很幸运。

因为在这两种情况下(我是否附加''\0 ''或不),如果我使用
函数atoi或任何其他字符串函数,编译器
不会发出警告或错误信息,发生了什么?



为什么要发出警告或错误?

-

Jeff

-je6543在yahoo.com


" Chapman"< ch ** @ hotmail.com>在消息新闻中写道:< il ***************** *** @ news-server.bigpond.net.au> ...

关于字符串的一些问题:这是我测试代码的提取部分:

/ * * ** /

char * arry;
arry = malloc(30 * sizeof(arry));


ITYM


arry = malloc(30 * sizeof * arry);


arry的类型是char *; * arry的类型是char。

arry [0] =''B'';
arry [1] =''S'';
arry [2] ='''';

/ ** ** /

问题是:

1.现在变量arry包含因为malloc的
而立即为null字符?

编号malloc()不会将分配的内存初始化为任何特定的

值。假设它包含垃圾。如果你需要在分配它时将内存初始化为0,请使用calloc()。

2.或者我应该在arry [3]附加''0'' ]? (事实上​​我接下来做了什么,但是当没有''\0''测试编译器时,它工作了(??)


运气。记忆你分配恰好被归零,但是你不能依赖这种行为。

因为在这两种情况下(我是否附加''\0'''或不),如果我使用
函数atoi或任何其他字符串函数,编译器
不会发出警告或错误信息,发生了什么?

提前致谢




字符串函数将从你指定的点开始遍历内存,直到它们看到一个nul终结符(0)。如果你忘了把

a nul终结符在你的缓冲区中,字符串函数将很高兴

读取或写入它直到它们找到一个或尝试读取或

写一个受保护或无效的内存地址,导致一个陷阱。


无论哪种方式,忘记将一个nul终止符附加到动态
分配缓冲区只有在程序运行时才会发生;你的编译器不能警告你不能用b $ b预测的东西。


Some questions about strings: this is the extracted piece of my test code:

/** **/

char *arry;
arry = malloc(30*sizeof(arry));

arry[0] = ''B'';
arry[1] = ''S'';
arry[2] = ''c'';

/** **/

The questions are:

1. Now does the variable arry contain the null character immediately because
of malloc?

2. or I should append ''0'' at arry[3] ? (which in fact what I did next, but
when testing the compiler without ''\0'', it worked (??)

Because in both cases (whether I append ''\0'' or not), if I use the
functions atoi or any other string functions, the compiler
does not give warning or error message, what happened?

Thanks in advance

解决方案

"Chapman" <ch**@hotmail.com> wrote:

Some questions about strings: this is the extracted piece of my test code:

/** **/

char *arry;
arry = malloc(30*sizeof(arry)); You allocate memory to hold 30 pointers-to-character, which is obviously
not your intention; make this:

arry = malloc( 30 * sizeof *arry );

arry[0] = ''B'';
arry[1] = ''S'';
arry[2] = ''c'';

/** **/

The questions are:

1. Now does the variable arry contain the null character immediately because
of malloc? No.

2. or I should append ''0'' at arry[3] ? If you intend to use it as a string: yes.
(which in fact what I did next, but
when testing the compiler without ''\0'', it worked (??) This was just (bad) luck; virtually *anything* could have happened.

Because in both cases (whether I append ''\0'' or not), if I use the
functions atoi or any other string functions, the compiler
does not give warning or error message, what happened?


The compiler cannot check something that gets allocated and altered at
run-time. You have to make sure your strings are ''\0''-terminated.

Regards

Irrwahn
--
Close your eyes and press escape three times.



"Chapman" <ch**@hotmail.com> wrote in message
news:il********************@news-server.bigpond.net.au...

Some questions about strings: this is the extracted piece of my test code:

/** **/

char *arry;
arry = malloc(30*sizeof(arry));
If you want to allocate a buffer to hold a string, you should write

arry = malloc(30); /* This way is correct. "sizeof char" is defined as 1
byte, you don''t need to use sizeof on this */

or

arry = malloc(30 * sizeof *arry) /* You should use sizeof on "*arry" (which
is a single char), not char* (which is a pointer. I think this is not what
you want, right ? ). Also, don''t use "sizeof( variable )" , use "sizeof
variable" or "sizeof(variable type)" instead. */


arry[0] = ''B'';
arry[1] = ''S'';
arry[2] = ''c'';

/** **/

The questions are:

1. Now does the variable arry contain the null character immediately because of malloc?
No. If you want to fill all the elements by zero after allocated, you can
consider "calloc"
2. or I should append ''0'' at arry[3] ?
Yes.

(which in fact what I did next, but when testing the compiler without ''\0'', it worked (??)

Don''t believe this, you are just lucky.
Because in both cases (whether I append ''\0'' or not), if I use the
functions atoi or any other string functions, the compiler
does not give warning or error message, what happened?



Why should it give warning or error ?
--
Jeff
-je6543 at yahoo.com


"Chapman" <ch**@hotmail.com> wrote in message news:<il********************@news-server.bigpond.net.au>...

Some questions about strings: this is the extracted piece of my test code:

/** **/

char *arry;
arry = malloc(30*sizeof(arry));
ITYM

arry = malloc (30 * sizeof *arry);

The type of arry is char*; the type of *arry is char.

arry[0] = ''B'';
arry[1] = ''S'';
arry[2] = ''c'';

/** **/

The questions are:

1. Now does the variable arry contain the null character immediately because
of malloc?

No. malloc() does not initialize the allocated memory to any specific
value. Assume it contains garbage. If you need the memory to be
initialized to 0 when allocating it, use calloc().
2. or I should append ''0'' at arry[3] ? (which in fact what I did next, but
when testing the compiler without ''\0'', it worked (??)
Luck. The memory you allocated just happened to be zeroed out, but
you can''t rely on that behavior.

Because in both cases (whether I append ''\0'' or not), if I use the
functions atoi or any other string functions, the compiler
does not give warning or error message, what happened?

Thanks in advance



The string functions will walk through memory starting at the point
you specify until they see a nul terminator (0). If you forget to put
a nul terminator in your buffer, the string functions will happily
read or write past it until they either find one or try to read or
write a protected or invalid memory address, causing a trap.

Either way, forgetting to append a nul terminator to a dynamically
allocated buffer is a condition that only occurs when the program is
run; your compiler can''t warn you about something that it can''t
predict.


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

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