[英]C: finding longest line
我正在尝试从(丹尼斯·里奇书)上运行此示例,但是找不到最长的行,有什么想法吗?
main.c
#include <stdio.h>
#include <stdlib.h>
#include "other.h"
int main()
{
/*printf("Hello world!\n");
printf("%d\n",getmynumber());
start of a new program%= */
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getLine(line, MAXLINE)) > 0 )
if (len > max){
max = len;
copyL(longest,line);
}
if (max > 0)
printf("%s", longest);
return 0;
}
其他
#include <stdio.h>
#include "other.h"
int getmynumber(void){
return 7;
}
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;
}
s[i] = '\0';
return i;
}
void copyL(char to[], char from[])
{
int i;
i = 0;
while ((to[i], from[i]) != '\0')
++i;
}
其他
#ifndef OTHER_H_INCLUDED
#define OTHER_H_INCLUDED
#define MAXLINE 1000
int getmynumber(void);
int getLine(char line[], int maxline);
void copyL(char to[], char from[]);
#endif // OTHER_H_INCLUDED
使用调试器:看起来我在copyL函数中没有增加...为什么有任何想法?
固定:
while ((to[i] = from[i]) != '\0'){
++i;
}
我认为您错误地转录了代码。 这行:
while ((to[i], from[i]) != '\0')
可能是不正确的。 表达式(to[i], from[i])
是逗号运算符的实例,该运算符产生其右侧作为结果值。 我认为这应该是一项任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.