char数组的问题 [英] problem with char array

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

问题描述




我正在阅读The C programming Language一书。我正在尝试做一个

的练习,要求找到最长的一行。我正在阅读

序列中的书,因此我对指针一无所知。


因为字符串是用char声明的string [n]",长度已经指定了
,如何存储任意长字符串?我已经尝试过

类似


char string [10];

....

char string2 [20];

string = string2;


但它抱怨不兼容的类型。如果我使用二维字符数组,因为它的总长度仍然是固定的。

$ b它也不会工作

$ b感谢您的帮助。

Pashmina

Hi,

I''m reading the book "The C programming Language". I''m trying to do an
exercise asking to find the longest line. I''m reading the book in
sequence hence I shouldn''t know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string? I''ve tried to do
something like

char string[10];
....
char string2[20];
string = string2;

but it complains about incompatible types. And it wouldn''t work either
if I use a 2 dimensional character array as the total length is still
fixed.

Thanks for help.
Pashmina

推荐答案

pa ******* @interfree.it (pashmina g。)写道:
pa*******@interfree.it (pashmina g.) writes:
我正在读这本书; C编程语言。我正在努力做一个要求找到最长线的练习。我正在按顺序阅读这本书,因此我对指针一无所知。

当一个字符串用char string [n]声明时,已经指定了长度,如何存储一个任意长的字符串?
I''m reading the book "The C programming Language". I''m trying to do an
exercise asking to find the longest line. I''m reading the book in
sequence hence I shouldn''t know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?




我认为你没有指针就可以了。但是,你不需要随时存储

a行,甚至是行的一部分,如果你只想找到

哪行是最长的。


马丁

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \ _ /



I don''t think you can without pointers. However, you don''t need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


Martin Dickopp写道:
Martin Dickopp wrote:
pa ******* @interfree.it (pashmina g。)写道:
pa*******@interfree.it (pashmina g.) writes:
我正在读这本书 C编程语言。我正在努力做一个要求找到最长线的练习。我正在按顺序阅读这本书,因此我对指针一无所知。

当一个字符串用char string [n]声明时,已经指定了长度,如何存储一个任意长的字符串?
我认为你没有指针就可以了。但是,如果你只想找到哪条线最长的话,你不需要随时存储一条线,甚至是一条线的一部分。
I''m reading the book "The C programming Language". I''m trying to do an
exercise asking to find the longest line. I''m reading the book in
sequence hence I shouldn''t know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?
I don''t think you can without pointers. However, you don''t need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.




这个技巧可以扩展,这样你就可以翻阅文件两次并打印

最长的一行,而不必存储它的任何部分。


-nrk。

Martin



The trick can be extended, so that you go through the file twice and print
the longest line without having to store any part of it.

-nrk.
Martin




-

删除电子邮件的devnull



--
Remove devnull for email


练习还要求打印出最长行的文本。那些

行是从标准输入读取的,我不知道有什么关于

读取或写入文件。事实上我不知道如何做到这一点

假设我应该只使用前一章中教导的那些东西

(变量和算术表达式,for,symbolic常数,字符

输入/输出,数组,函数和字符数组)。


Pashmina


On Thu ,2004年2月26日13:14:45 +0100

Martin Dickopp< ex **************** @ zero-based.org>写道:
The exercise also asks to print out the text of the longest line. Those
lines are read from standard input, I shouldn''t know anything about
reading nor writing a file yet. In fact I''ve no idea of how to do that
assuming that I should use only those things taught in previous chapter
(variables and arithmetic expressions, for, symbolic constant, character
input/output, array, function and character arrays).

Pashmina

On Thu, 26 Feb 2004 13:14:45 +0100
Martin Dickopp <ex****************@zero-based.org> wrote:
pa ******* @interfree.it (pashmina g。)写道:
pa*******@interfree.it (pashmina g.) writes:
我正在阅读The C programming Language一书。我正在努力做一个要求找到最长线的练习。我正在按顺序阅读这本书,因此我对指针一无所知。

当一个字符串用char string [n]声明时,已经指定了长度,如何存储一个任意长的字符串?
I''m reading the book "The C programming Language". I''m trying to do
an exercise asking to find the longest line. I''m reading the book in
sequence hence I shouldn''t know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?



我认为你没有指针就可以了。但是,如果您只想知道哪条线路最长,您不需要随时存储线路甚至是线路的一部分。
<马丁

-
, - 。 Martin Dickopp,德国德累斯顿,=,-_-。
=。
/, - ) http://www.zero-based.org/ ((_ /)o
o(\ _))\` - ''
- ''(。)` - ''
` - 。 Debian,GNU操作系统的一种变体。 \ _ /



I don''t think you can without pointers. However, you don''t need to
store a line, or even part of a line, at any time, if you only want to
find out which line is the longest.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-.
=.
/ ,- ) http://www.zero-based.org/ ((_/)o
o(\_))\ `-''
`-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/



-


--


这篇关于char数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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