C:寻找最长的线

标签 c linux

我正在尝试从(dennis ritchie book)运行这个例子,但是它没有找到最长的线,知道吗?

ma​​in.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 函数中没有递增......有什么想法吗?

code blocks debug

修复:

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/

相关文章:

linux - Bash 将大文件拆分为较小的文件

linux - 如何在 IntelliJ/WebStorm/PhpStorm 中从 "external tools"运行 sudo 脚本?

linux - Valgrind、Helgrind 使用 >90% 的 CPU 并且不产生结果

linux - 安装并重新启动和 plonectl 启动后站点不可用

C——从调用函数中释放内存

c - 为什么 getchar() 不停止读取 C 中的字符串?

c++ - 如果使用 LoadLibrary 显式链接 dll,则列出从 PE 文件导入的函数

linux - vagrant、centos、dhclient 的问题

c - 案例转换程序的实现改进

c++ - #pragma once 添加包含保护吗?