我试图从(丹尼斯里奇书)中运行这个例子,但它没有找到最长的线,任何想法?
的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;
}
other.c
#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;
}
other.h
#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函数中没有增加...任何想法为什么?
FIX:
while ((to[i] = from[i]) != '\0'){
++i;
}
答案 0 :(得分:3)
我认为您错误地转录了代码。这一行:
while ((to[i], from[i]) != '\0')
可能不正确。表达式(to[i], from[i])
是逗号运算符的一个实例,它将右侧生成为结果值。我认为它应该是一项任务。