我正在尝试从(dennis ritchie book)运行这个例子,但是它没有找到最长的线,知道吗?
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;
}
其他.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;
}
其他.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 函数中没有递增......有什么想法吗?
修复:
while ((to[i] = from[i]) != '\0'){
++i;
}
最佳答案
我认为您错误地转录了代码。这一行:
while ((to[i], from[i]) != '\0')
可能不正确。表达式 (to[i], from[i])
是逗号运算符的一个实例,它产生右侧作为结果值。我认为这应该是一项作业。
关于C:寻找最长的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821326/