繁体   English   中英

为什么我调用 function 时程序会停止?

[英]Why does my program stop when I call a function?

我正在编写一个 function ,它应该反转给定的单词或输入,但是当我调用它时程序会继续停止。

代码是:

#include <stdio.h>
#define MAX 1000

void reverse(char normal[], char reversed[]);

main() {

    int c, i;
    char line[MAX], reversed_line[MAX];

    for (i=0; (c = getchar()) != EOF && c != '\n'; i++) {
        line[i] = c;
        if (c == '\n')
            i++;
    }
    line[i] = '\0';
    copy(line, reversed_line);
    printf("%s", reversed_line);

    return 0;
}


void reverse(char normal[], char reversed[]) {

    int i, len;

    for (i = 0; normal[i] != '\0'; i++)
        len++;
        
    i = 0;
    while ((reversed[len-i] = normal[i]) != '\0')
        i++;
}

程序停止的行是copy(line, reversed_line); 它给出的错误是[Error] ld returned 1 exit status

任何帮助将不胜感激

首先你不执行你的程序只尝试编译和链接它。

错误消息表明链接失败,因为 linker has not found the function copy ,实际上从未在您的代码中定义。 标准库中没有“副本” function。 如果你想复制字符串或一些 memory 区域,你应该使用strcpystrncpymemcpymemmove

但即使程序链接你的代码也有很多问题。

这是该程序的工作版本:

#include <stdio.h>
#define MAX 5

char *reverse(char *normal, char *reversed);

int main(void) {

    int c;
    size_t i = 0;
    char line[MAX], reversed_line[MAX];

    while((c = getchar()) != EOF)
    {
        line[i] = c;
        if(c != '\n') i++;
        if(i >= MAX - 1) break;
    }
    line[i] = '\0';

    reverse(line, reversed_line);
    printf("%s", reversed_line);

    return 0;
}

char *reverse(char *normal, char *reversed) 
{
    size_t size = 0;

    if(normal && reversed)
    {
        while(normal[size]) size++;
        reversed[size] = 0;
        while(size)
        {
            reversed[size - 1] = *normal++;
            size--;
        }
    }
    return reversed;
}

https://godbolt.org/z/jb7b6hjfT

对于此 for 循环中的初学者

for (i=0; (c = getchar()) != EOF && c != '\n'; i++) {
    line[i] = c;
    if (c == '\n')
        i++;
}

您没有检查i是否大于或等于MAX 而且这个 if 语句

    if (c == '\n')
        i++;

由于循环的条件,永远不会执行。

您声明了 function reverse

void reverse(char normal[], char reversed[]);

但是您尝试调用另一个 function copy而不是 function 。

copy(line, reversed_line);

function 内的这个循环也reverse

while ((reversed[len-i] = normal[i]) != '\0')
        i++;

正在尝试在参数reversed所指向的字符数组的第一个 position 中写入终止零字符'\0'

您需要的是以下内容

#include <stdio.h>

#define MAX 1000

char * reverse_copy( const char normal[], char reversed[] );

int main(void) 
{
    char line[MAX], reversed_line[MAX];

    size_t i = 0;
    
    for  ( int c; i + 1 < MAX && ( c = getchar() ) != EOF && c != '\n'; i++ ) 
    {
        line[i] = c;
    }
    
    line[i] = '\0';
    
    puts( reverse_copy( line, reversed_line ) );
    
    return 0;
}

char * reverse_copy( const char normal[], char reversed[] ) 
{
    size_t len = 0;

    while ( normal[len]  != '\0' ) ++len;

    reversed += len;
    
    *reversed = '\0';
    
    for ( ; len != 0; --len )
    {
        *--reversed = *normal++;
    }

    return reversed;
}

如果输入例如字符串

Hello World!

那么程序 output 将是

!dlroW olleH

您在代码中做了一些不必要的事情。 例如,当您已经知道长度时,为什么在您的 while 循环中reverse设置此条件?

while ((reversed[len - i] = normal[i]) != '\0')

我已经重写了你的整个代码来优化一些东西。 也许这是对0___________ 答案的有益补充。 我在代码中包含了很多解释性注释。

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

#define MAX 1000

void reverse(char *str, char *rev_str, size_t len)
{
    /* check if `str` is a valid string */
    if (str == NULL) {
            fprintf(stderr, "Error: invalid string\n");
            exit(EXIT_FAILURE);
    }

    /* take null character into account */
    for (int i = 0; i < len - 1; ++i)
            rev_str[len - i - 2] = str[i];
    rev_str[len - 1] = '\0';
}

int main(void)
{
    char str[MAX];
    fputs("Input: ", stdout);
    /* just use `fgets` and prevent buffer overflow */
    fgets(str, MAX, stdin);

    /* `len` includes null character */
    size_t len = strlen(str);
    /* change '\n' to '\0' */
    str[len - 1] = '\0';

    /* `sizeof(rev_str)` doesn't always need to be `MAX` */
    char rev_str[len];
    reverse(str, rev_str, len);
    printf("Output: %s\n", rev_str);

    return EXIT_SUCCESS;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM