K& R Ex 1-22 [英] K&R Ex 1-22

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

问题描述

我正在尝试完成K& R书中的练习1-23,该书需要

a程序来包装给定列的输入行,确保处理

包裹两次或不包含空格的行。这就是我到目前为止* b $ b *:
http:/ /pastebin.com/f61c6cfc

*插入是一个函数,它将字符串的内容向右移动

一个字符并在其位置插入另一个字符。


我肯定有一个更好的方法可以做到这一点,但是我很难/ b $ b来重写它另一种方式。


无论如何,我真正想知道的是

为什么变量search_state总是等于零(至少根据我放在那里的printf语句和/或
包装的事实,即使是在< ;>
包裹之前的事实),即使在search_state = 1之前的if语句;解决了

是真的。这几乎就像search_state在for循环结束时在

中失去了价值一样。如果它没有被设置为1,那么没有包装将会发生



因为这么多而感到非常愚蠢
$ b这本书很早就有一个练习麻烦了,但这让我感到困惑。

困惑。

-

你可以获得生活中的一切想要,如果你能帮助足够的其他人b / b
得到他们想要的东西。

I''m trying to complete excercise 1-23 in the K&R book, which calls for
a program to wrap input lines at a given column, making sure to handle
lines that wrap twice or don''t contain whitespace. This is what I have
so far*:
http://pastebin.com/f61c6cfc
*Insert is a function which shifts the contents of the string right by
one character and inserts another character in its place.

I''m sure there''s probably a completely better way to do this, but I''m
too far into it to rewrite it a different way.

Anyway, what I really want to know is
why the variable "search_state" always equals zero (at least according
to the printf statement I put in there and the fact that it never
wraps) even though the if statement before "search_state = 1" resolves
to be true. It''s almost as though search_state was losing its value at
the end of the for loop. If it doesn''t get set to 1, no wrapping will
happen.

I feel really stupid for having this much
trouble with an excercise so early in the book, but this has me
baffled.
-
You can get everything in life you want, if you will help enough other
people get what they want.

推荐答案

Andrew C.说:
Andrew C. said:

我正在尝试在K& R书中完成练习1-23,这需要

a程序来包装输入行给定列,确保处理包含两次或不包含空格的
行。这就是我到目前为止* b $ b *:
http:/ /pastebin.com/f61c6cfc



我现在没有多少时间,但稍后我会的。对我来说(或者确实

为其他人)以足够的细节来看你的问题来帮助你,

如果你能发布完整的,可编辑的来源会很方便您正在使用的代码

,最好是在Usenet上,而不是在网站上。
网站。此外,了解您正在使用的测试数据非常有用。


我的第一眼(它*只是快速浏览一下)表明,当你

传递一个少于80个字符的字符串,''next''的值将是

0,这看起来可能有问题。 (这可能不是

问题 - 这只是第一印象。)


毫无疑问,其他人将能够为您提供更多帮助,但我确实这样做了

建议你在这里发布完整的资源,连同你的测试

数据。


HTH。手。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件: -http:// WWW。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

I haven''t much time right now, but I will have, later on. For me (or indeed
for anyone else) to look at your problem in sufficient detail to help you,
it would be handy if you could post the complete, compilable source code
that you are using, preferably right here on Usenet rather than on a Web
site. Also, it would be very useful to know what test data you''re using.

My first glance (and it *is* only a quick glance) suggests that, when you
pass a string of fewer than 80 characters, your value for ''next'' will be
0, which looks like it could be problematic. (That might not be the
problem - it''s just a first impression.)

No doubt someone else will be able to help you more, but I really do
recommend that you post the full source here, together with your test
data.

HTH. HAND.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


2008-08-19,Richard Heathfield< rj*@see.sig.invalidwrote:
On 2008-08-19, Richard Heathfield <rj*@see.sig.invalidwrote:

Andrew C.说:
Andrew C. said:

>我正在尝试在K& R书中完成练习1-23,这要求
一个程序,用于包含给定列的输入行,确保处理包裹两次或不包含空格的行。这就是我到目前为止*:
http://pastebin.com / f61c6cfc



我现在没有多少时间,但稍后我会的。对我来说(或者确实

为其他人)以足够的细节来看你的问题来帮助你,

如果你能发布完整的,可编辑的来源会很方便您正在使用的代码

,最好是在Usenet上,而不是在网站上。
网站。此外,了解您正在使用的测试数据非常有用。


我的第一眼(它*只是快速浏览一下)表明,当你

传递一个少于80个字符的字符串,''next''的值将是

0,这看起来可能有问题。 (这可能不是

问题 - 这只是第一印象。)


毫无疑问,其他人将能够为您提供更多帮助,但我确实这样做了

建议你在这里发布完整的资源,连同你的测试

数据。


HTH。 HAND。


I haven''t much time right now, but I will have, later on. For me (or indeed
for anyone else) to look at your problem in sufficient detail to help you,
it would be handy if you could post the complete, compilable source code
that you are using, preferably right here on Usenet rather than on a Web
site. Also, it would be very useful to know what test data you''re using.

My first glance (and it *is* only a quick glance) suggests that, when you
pass a string of fewer than 80 characters, your value for ''next'' will be
0, which looks like it could be problematic. (That might not be the
problem - it''s just a first impression.)

No doubt someone else will be able to help you more, but I really do
recommend that you post the full source here, together with your test
data.

HTH. HAND.



你是对的,我应该发布完整的源代码。我使用了

pastebin因为我认为它会更大的违反

ettiquette将一个相当大的源文件直接发布到usenet,长

行和所有,但这里是:


#include< stdio.h>

#include< string.h>


#define MAXCOLS 80 / *要换行的列* /

#define MAXLINE 1000 / *最大字符串长度* /

#define NEXT(当前)((当前/ MAXCOLS + 1)* MAXCOLS)/ *宏

计算下一个* /


void wrap(char s [],int len );

void insert(char s [],int at,char c);

int getline(char s [],int lim);


main()

{

char line [MAXLINE];

int len;


int i;

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

putchar('''');

printf(" | | \ n");


while((len = getline(line,MAXLINE))0){

wrap(line,len);

printf("%s",line);

}


返回0;

}


/ * wrap:自动换行所需长度的字符串* /

void wrap(char s [],int len)

{

int search_state = 0; / * 0 =寻找下一个,1 =寻找

空白* /

int i = len-1;

int next =( (I / MAXCOLS))* MAXCOLS; / *下一栏包裹在* /


for(i = len-1; i MAXCOLS-1; - i){

next =( (i / MAXCOLS))* MAXCOLS;

printf("%d",search_state); / * debug * /

if(search_state == 1){

if(s [i] ==''''){/ * all are well * /

s [i] =''\ n''; / *在$ / b $ b空格中添加换行符* /

search_state = 0; / *重置状态* /

}

else if(i == next&& s [i]!=''''){/ *那里

不是任何空格* /

insert(s,i,''\ n''); / *在换行点插入换行符

* /

search_state = 0; / *重置状态* /

}

}

else

if(next> = MAXCOLS& ;& i == next)/ *如果接下来有

a,我等于它,开始包装* /

search_state = 1;

}

}


/ * insert:在字符串中间插入一个字符* /

void insert (char s [],int at,char c)

{

int i;


for(i = strlen( s)-1; i> = at; - i)

s [i + 1] = s [i];

s [i] = c;

}


/ * getline:读取一行到s,返回长度* /

int getline(char s [],int lim )

{

int c,i;

for(i = 0; i< lim-1&&(c = getchar) ())!= EOF&& c!=''\ n''; ++ i)

s [i] = c;

if(c ==''\ n''){

s [i] = c;

++ i;

}

if(i> = lim-1){

while((c = getchar())!= EOF&& c!=''\ n'')

++ I; / *在溢出的情况下增加长度* /

s [lim-1] =''\ 0'';

}

否则

s [i] =''\ 0'';

返回i;

}

By这样,任何输入行都少于80个字符,绝对不会发生任何事情,这确实是正确的行为

因为不需要自动换行。


至于测试数据,任何输入(我要么捣碎键盘或输入

歌词)都完全相同:吐出输入不变。


感谢您的帮助,对不起是一个菜鸟。

-

你可以得到你想要的一切,如果你能得到足够的帮助,那么人们可以得到他们想要的东西。

You''re right, I should have posted the full source code. I used
pastebin because I figured it would have been a bigger breach of
ettiquette to post a sizeable source file directly to usenet, long
lines and all, but here goes:

#include <stdio.h>
#include <string.h>

#define MAXCOLS 80 /*Column to wrap at*/
#define MAXLINE 1000 /*Maximum string length*/
#define NEXT(current) ((current/MAXCOLS+1)*MAXCOLS) /*macro
to calculate next*/

void wrap(char s[], int len);
void insert(char s[], int at, char c);
int getline(char s[], int lim);

main()
{
char line[MAXLINE];
int len;

int i;
for(i = 0; i<=80; ++i)
putchar('' '');
printf("|\n");

while((len = getline(line, MAXLINE)) 0) {
wrap(line,len);
printf("%s",line);
}

return 0;
}

/*wrap: word wrap a string at the desired length */
void wrap(char s[], int len)
{
int search_state = 0; /*0 = looking for next, 1 = looking for
whitespace*/
int i = len-1;
int next = ((i/MAXCOLS))*MAXCOLS; /*next column to wrap at*/

for(i = len-1; i MAXCOLS-1; --i) {
next = ((i/MAXCOLS))*MAXCOLS;
printf(" %d ",search_state); /*debug*/
if(search_state == 1) {
if(s[i] == '' '') { /*all is well*/
s[i] = ''\n''; /*place a newline in the
whitespace*/
search_state = 0; /*reset the state*/
}
else if(i == next && s[i] != '' '') { /*there
wasn''t any whitespace*/
insert(s,i,''\n''); /*insert a newline
at the wrap point*/
search_state = 0; /*reset the state*/
}
}
else
if(next >= MAXCOLS && i == next) /*if there is
a next, and i equals it, start wrapping*/
search_state = 1;
}
}

/*insert: insert a character into the middle of a character string*/
void insert(char s[], int at, char c)
{
int i;

for(i = strlen(s)-1; i>=at; --i)
s[i+1] = s[i];
s[i] = c;
}

/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i<lim-1 && (c=getchar()) != EOF && c!=''\n''; ++i)
s[i] = c;
if (c == ''\n'') {
s[i] = c;
++i;
}
if (i >= lim-1) {
while((c = getchar()) != EOF && c!=''\n'')
++i; /*increment length anyway in case of overflow*/
s[lim-1] = ''\0'';
}
else
s[i] = ''\0'';
return i;
}
By the way, with any input line less than 80 characters, absolutely
nothing should happen to it, which is indeed the correct behavior
because no word wrapping is needed.

As for test data, any input (I either mashed the keyboard or typed in
song lyrics) does the exact same thing: spit the input out unchanged.

Thanks for your help, and sorry for being a noob.
--
You can get everything in life you want, if you will help enough other
people get what they want.


Andrew C.说:
Andrew C. said:

2008-08-19,Richard Heathfield< rj*@see.sig.invalidwrote:
On 2008-08-19, Richard Heathfield <rj*@see.sig.invalidwrote:

>>
不,你其他人可以帮助你更多,但我确实建议你在这里发布完整的来源,以及你的测试数据。
>>
No doubt someone else will be able to help you more, but I really do
recommend that you post the full source here, together with your test
data.



你说得对,我应该发布完整的源代码。我使用了

pastebin因为我认为它会更大的违反

ettiquette将一个相当大的源文件直接发布到usenet,长

行和所有,

You''re right, I should have posted the full source code. I used
pastebin because I figured it would have been a bigger breach of
ettiquette to post a sizeable source file directly to usenet, long
lines and all,



您对礼仪的关注是一个值得欢迎的改变!尽管如此,如今在Usenet上一百美元左右的价格并不是什么大不了的事,至少对于大多数人而言并非如此,我们可以希望这篇文章中的前几篇文章线程

将警告调制解调器用户关闭。 :-)


好​​的,我的第一步是编译它:


gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align

-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes

-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef

-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings

-ffloat-store -fno-builtin -O2 -g -pg -c -o kr123.o kr123 .c

kr123.c:14:警告:return-type默认为`int''

kr123.c:14:警告:函数声明不是原型

kr123.c:在函数`wrap'':

kr123.c:50:警告:由于
原型

kr123.c:函数`getline'':

kr123.c:75:警告:`c''可能未使用初始化在这个功能中


所以,让我们去解决这些问题。


在第14行,我改变了:


main()


to:


int main(无效)


在第50行,''\ n''实际上是int类型,你愿意吗?相信?但是我们知道

它的值必须符合char(有几个原因,其中一个原因是它是b / b
是源字符集的强制成员,所以它必须在1到CHAR_MAX的范围内有一个值

,所以可以将它传递给函数

期待一个char。无需修复(尽管更改insert()以获取int

而不是char将至少使警告静音)。


到目前为止,最严重的警告是关于''c''可能被使用的那个

未初始化。但据我所知,这实际上是不可能的,因为

只有一次调用getline,并且它为lim指定了一个大的值

,所以我< lim-1必须是第一次真实的,所以getchar是绝对称为



编译器这么多。让我们来看看这个行为。


为了让事情变得不那么冗长,我将MAXCOLS更改为12并重新编译了
。我给你的程序输入了abcdefghijklmnop,并且它按照预期打印了一堆空格和一个垂直条。
(我不知道

为什么你这样做,但是,嘿,这是你的程序!)然后吐出看起来像一些调试数字的b $ b,然后是abcdefghijklmnop,没有

换行。


然后我给了它很多垃圾,并且(记得我们改变了

MAXCOLS到12)看看发生了什么!


fkjslvnfdlskjvnfsdkjlvnfdskjvnfsdkjvnfksldjnvjksld fnvjdskfvnfdjsvnjkdfls

jfks vjksfdvjkfsdhvjksdfvhjksdfv

0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0

0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 fkjslvnfdls

jjvnfsdkjlvnfdskjvnfsdkj

nnfksldjnvjksldfnvjdskfv

ffdjsvnjkdfls jfks vjksf

vvjkfsdhvjksdfvhjksdfv

#1 Wed Mar 27 13:57:05 UTC 2002


哇!我不知道那个时间戳来自哪里...... :-)


所以看起来你*正在*得到包装,但不是你想要的方式和时间。

看起来你还有一个未终止的字符串。 (这是通常的完全离开输出的原因。)


阅读代码的时间。


main中的前几行只打印出一堆空格和一个|,对于

没有明显的理由 - 尽管毫无疑问你知道为什么。没有非法的

那么,让我们继续前进。


你有一个很好的简单循环,包括getline,wrap和printf。

printf打印''line'',因此我们很清楚哪个'string''没有

被终止。我检查了它是否构造得正确,

通过查看getline,并且getline看起来很好。


因此未能终止的问题是wrap()或者插入(),

和我的钱在insert()上,所以让我们来看看:


void insert( char s [],int at,char c)

{

int i;


for(i = strlen(s) )-1; i> = at; - i)

s [i + 1] = s [i];

s [i] = c;

}


请考虑以下字符串:ABCDEF \ n \ 0" (我已明确地将\0作为明确的

,但它不是strlen给出的计数的一部分。)


i = strlen (s) - 1;由于strlen(s)是7(AF是6,+ \ n),我取值

6.


s [i + 1] = s [i];


所以我们正在做[7] = s [6]。现在,s [7] /是/ null终止符,但我们

只是覆盖了它。不好。让我们解决它。


虽然strlen产生一个字符串的长度(不包括null),但我们

可以用另一种方式来考虑它,即返回null

终止符的偏移量。

void insert(char s [],int at,char c)

{

size_t nullpos = strlen(s);

size_t pos = nullpos;

while(pos 0&& pos at)

{

s [pos] = s [pos - 1];

--pos;

}

s [pos] = c;

s [nullpos + 1] =''\ 0'';

}


这解决了我们的空终止问题。现在对于逻辑错误,在wrap()函数中显然是

。实验很快就发现你的代码*包裹了*,虽然相当奇怪 - 例如,第一个包裹

似乎发生在2 * MAXCOLS,而不是MAXCOLS。


看起来你要做的就是用换行符替换一个空格

如果可能的话,但是如果有必要,你会转移一个

一个字符插入一个换行符。一个非常合理的方法,

虽然你当然要小心接近你的

缓冲区的大小限制。


让我觉得这样做要容易得多:


void wrap(char s [],size_t len)

{

char * t = s;

while(len> = MAXCOLS)

{

t + = MAXCOLS ;

while(ts&& * t!='''')

{

--t;

}

if(* t =='''')

{

* t =''\ n' ';

}

其他

{

t + = MAXCOLS;

insert (t,0,''\ n'');

}

len - =(t - s);

s = t ;

}

}


将 - 以及insert()更新 - 删除到您的程序中(以及制作

适当的类型 - 从int更改为size_t,这是一个更合适的描述缓冲区大小的
类型,用于输入像这样:


这解决了我们的空终止问题。现在对于逻辑错误,在wrap()函数中显然是

。实验很快就发现你的代码*包裹了*,虽然相当奇怪 - 例如,第一个包裹

似乎发生在2 * MAXCOLS,而不是MAXCOLS。


我得到这样的输出:


这是固定的

我们的空

终止

问题。

现在为

逻辑错误,

这是

清楚地在

包裹()

功能。

Experimenta

tion

快速

透露

你的

代码*是*

包裹,

虽然

而非

奇怪 -

for

例如,

第一个

换行似乎

发生在

2 * MAXCOLS,

而不是

at

MAXCOLS。


这似乎很好用。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-h TTP:// WWW。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Your concern for etiquette makes a welcome change! Nevertheless, a hundred
lines or so is no big deal in Usenet nowadays, at least not for most
people, and we can hope that the first couple of articles in this thread
will warn modem-users off. :-)

Okay, my first step was to compile it:

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-ffloat-store -fno-builtin -O2 -g -pg -c -o kr123.o kr123.c
kr123.c:14: warning: return-type defaults to `int''
kr123.c:14: warning: function declaration isn''t a prototype
kr123.c: In function `wrap'':
kr123.c:50: warning: passing arg 3 of `insert'' with different width due to
prototype
kr123.c: In function `getline'':
kr123.c:75: warning: `c'' might be used uninitialized in this function

So let''s go fix those things.

On line 14, I changed:

main()

to:

int main(void)

On line 50, ''\n'' is actually of type int, would you believe? But we know
its value must fit in a char (for several reasons, one of which is that it
is a mandatory member of the source character set, so it must have a value
in the range 1 to CHAR_MAX), so it''s okay to pass it to a function
expecting a char. No fix required (although changing insert() to take int
rather than char would at least silence the warning).

By far the most serious warning is the one about ''c'' possibly being used
uninitialised. But as far as I can see, this is actually impossible, given
that there is only one call to getline, and it specifies a large-ish value
for lim, so i<lim-1 must be true first time round, so the getchar is
definitely called.

So much for the compiler. Let''s look at the behaviour.

To make matters a little less long-winded, I changed MAXCOLS to 12 and
recompiled. I gave your program an input of abcdefghijklmnop, and it
printed a bunch of spaces and a vertical bar, as expected. (I don''t know
why you do that, but hey, it''s your program!) It then spat out what look
like some debugging numbers, followed by abcdefghijklmnop again, with no
wrap.

Then I gave it lots and lots of junk, and (remembering that we changed
MAXCOLS to 12) look what happened!

fkjslvnfdlskjvnfsdkjlvnfdskjvnfsdkjvnfksldjnvjksld fnvjdskfvnfdjsvnjkdfls
jfks vjksfdvjkfsdhvjksdfvhjksdfv
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 fkjslvnfdls
jjvnfsdkjlvnfdskjvnfsdkj
nnfksldjnvjksldfnvjdskfv
ffdjsvnjkdfls jfks vjksf
vvjkfsdhvjksdfvhjksdfv
#1 Wed Mar 27 13:57:05 UTC 2002

WOW! I have no idea where that timestamp came from... :-)

So it looks like you *are* getting wrap, but not how and when you want it.
It also looks like you have an unterminated "string". (That''s the usual
cause of utterly off-the-wall output.)

Time to read the code.

The first few lines in main just print out a bunch of spaces and a |, for
no apparent reason - although no doubt you know why. Nothing illegal
there, so let''s move on.

You have a nice simple loop, consisting of getline, wrap, and printf. The
printf prints ''line'', so we have a good idea of which "string" isn''t
getting terminated. I checked to see whether it is constructed properly,
by looking at getline, and getline seems fine.

So the problem of failure to terminate is either in wrap() or in insert(),
and my money is on insert(), so let''s take a look:

void insert(char s[], int at, char c)
{
int i;

for(i = strlen(s)-1; i>=at; --i)
s[i+1] = s[i];
s[i] = c;
}

Consider the following string: "ABCDEF\n\0" (I''ve made the \0 explicitly
visible, but it isn''t part of the count given by strlen.)

i = strlen(s) - 1; Since strlen(s) is 7 (A-F is 6, + \n), i takes the value
6.

s[i+1] = s[i];

so we''re doing s[7] = s[6]. Now, s[7] /was/ the null terminator, but we
just overwrote that. Not good. Let''s fix it.

Whilst strlen yields the length of a string (not including the null), we
can think of it in another way, i.e. as returning the offset of the null
terminator.

void insert(char s[], int at, char c)
{
size_t nullpos = strlen(s);
size_t pos = nullpos;
while(pos 0 && pos at)
{
s[pos] = s[pos - 1];
--pos;
}
s[pos] = c;
s[nullpos + 1] = ''\0'';
}

That''s fixed our null termination problem. Now for the logic bug, which is
clearly in the wrap() function. Experimentation quickly revealed that your
code *is* wrapping, albeit rather bizarrely - for example, the first wrap
seems to occur at 2*MAXCOLS, rather than at MAXCOLS.

It appears that what you''re trying to do is replace a space with a newline
if possible, but if necessary you''ll shift a long string''s tail over by
one character to insert a newline. A perfectly reasonable approach,
although of course you have to be careful with lines close to your
buffer''s size limit.

It strikes me that it would be much easier to do this as follows:

void wrap(char s[], size_t len)
{
char *t = s;
while(len >= MAXCOLS)
{
t += MAXCOLS;
while(t s && *t != '' '')
{
--t;
}
if(*t == '' '')
{
*t = ''\n'';
}
else
{
t += MAXCOLS;
insert(t, 0, ''\n'');
}
len -= (t - s);
s = t;
}
}

Dropping that - and the insert() update - into your program (and making
appropriate type-changes from int to size_t, which is a more appropriate
type for describing buffer sizes), for input like this:

That''s fixed our null termination problem. Now for the logic bug, which is
clearly in the wrap() function. Experimentation quickly revealed that your
code *is* wrapping, albeit rather bizarrely - for example, the first wrap
seems to occur at 2*MAXCOLS, rather than at MAXCOLS.

I get output like this:

That''s fixed
our null
termination
problem.
Now for the
logic bug,
which is
clearly in
the wrap()
function.
Experimenta
tion
quickly
revealed
that your
code *is*
wrapping,
albeit
rather
bizarrely -
for
example,
the first
wrap seems
to occur at
2*MAXCOLS,
rather than
at
MAXCOLS.

and that seems to be working nicely.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于K&amp; R Ex 1-22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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