读动态字符串 [英] Read dynamic string

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

问题描述

我使用此代码读取动态字符串:


char * s1;

.......

puts(Inserire una stringa:);

while((* s1 ++ = getchar())!=''\ n'');

* s1 =''\''';


编译(ANSI C)没问题但我在执行过程中收到错误。


如果我使用静态数组,则问题不存在。


我找不到错误。

I use this code to read dynamic string:

char *s1;
.......
puts("Inserire una stringa: ");
while((*s1++=getchar())!=''\n'');
*s1=''\0'';

The compilation (ANSI C) is OK but I receive an error during the execution.

The problem is not present if I use a static array.

I cannot find the error.

推荐答案



" Sillaba atona" < NO **** @ tin.it>在消息中写道

"Sillaba atona" <NO****@tin.it> wrote in message
char * s1;
......
puts(Inserire una stringa:);
while((* s1 ++) = getchar())!=''\ n'');
* s1 =''\''';

编译(ANSI C)没问题,但我收到了
执行期间出错。

如果我使用静态数组,则问题不存在。

我找不到错误。
char *s1;
......
puts("Inserire una stringa: ");
while((*s1++=getchar())!=''\n'');
*s1=''\0'';

The compilation (ANSI C) is OK but I receive an error during the
execution.

The problem is not present if I use a static array.

I cannot find the error.



s1指向无处。然后你开始用字符覆盖这个随机的

内存位置。通常结果将是崩溃

(可能是一条消息说分段错误)。

如果你说s1 = malloc(1000)那么s1将指向1000为你保留的字符,你的节目不会崩溃,直到你超过这个数字。 (你可以保持

a计数并调用realloc(),如果你想能够读取任意的

字符串,只要计算机的meory允许)。


s1 points to nowhere in particular. You then start overwriting this random
memory location with characters. Usually the result will be a crash
(probably a message saying "segmentation fault").
If you say s1 = malloc(1000) then s1 will point to 1000 chars reserved for
you, and the program won''t crash until you exceed that figure. (You can keep
a count and call realloc(), if you want to be able to read an arbitrary
string as long as the computer''s meory allows).


文章< 43 *********************** @ reader1.news.tin。它>,

Sillaba atona< NO **** @ tin.it>写道:
In article <43***********************@reader1.news.tin.it>,
Sillaba atona <NO****@tin.it> wrote:
我用这段代码来读取动态字符串:
char * s1;
......
puts(Inserire una stringa: ;);
while((* s1 ++ = getchar())!=''\ n'');


您尚未分配任何存储空间。 s1是一个未初始化的指针;

你必须先将它指向一块内存才能使用该代码。


除了那个代码没有''考虑到

a真正长字符串的可能性,如果你预分配内存,那么无论你分配多少钱,用户都可能输入更大的内容。

因此你需要限制你要支付的长度

注意,否则你需要使用分配的方案

内存根据需要增长。

* s1 =''\''';
编译(ANSI C)没问题但我在执行期间收到错误。


一个好的编译器会警告s1可能没有初始化,但

标准不需要这样的警告。

如果我使用静态数组,则问题不存在。
I use this code to read dynamic string: char *s1;
......
puts("Inserire una stringa: ");
while((*s1++=getchar())!=''\n'');
You have not allocated any storage. s1 is an uninitialized pointer;
you have to point it to a block of memory before you can use that code.

Except that that code doesn''t take into account the possibility of
a really long string, and if you pre-allocate the memory then no matter
how much you allocate, the user might enter something larger.
Therefore you either need to limit the length that you will pay
attention to, or else you need to use a scheme in which the allocated
memory is grown as needed.
*s1=''\0''; The compilation (ANSI C) is OK but I receive an error during the execution.
A good compiler would warn that s1 was potentially uninitialized, but
such a warning is not -required- by the standard.
The problem is not present if I use a static array.




更改回静态,然后让用户粘贴(比如说)

32K的文字,看看当你使用静态数组时是否仍然说问题是不存在



-

没有人有权通过要求经验证据来摧毁另一个人的信念。 - Ann Landers



Change back to a static, and then have the user paste in (say)
32K of text, and see whether you still say the problem is "not present"
when you use a static array.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


Malcolm写道:
Malcolm wrote:
如果你说s1 = malloc(1000)那么s1将指向1000个字符为你保留,程序不会崩溃,直到你超过这个数字。 (你可以保持计数并调用realloc(),如果你想能够读取任意的
字符串,只要计算机允许的话。)
If you say s1 = malloc(1000) then s1 will point to 1000 chars reserved for
you, and the program won''t crash until you exceed that figure. (You can keep
a count and call realloc(), if you want to be able to read an arbitrary
string as long as the computer''s meory allows).



或者你可以继续阅读未分配的缓冲区,直到你的电脑喵喵叫。


;-)


with尊重,

Toni Uusitalo



Or you can keep reading into unallocated buffer until your computer meows.

;-)

with respect,
Toni Uusitalo


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

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