【问题标题】:Pass by reference to a recursive function [closed]通过引用传递给递归函数[关闭]
【发布时间】:2018-06-09 23:03:05
【问题描述】:
#include <stdio.h>

void word(char a[100], int i, int *max, int *c)
{
    printf("%d  %d", max , c);
    int length = 0;
    while (i < strlen(a) && a[i] != ' ')
    {
        length++;
        //printf("%c\n", a[i]);
        i++;
    }
    if (a[i] == ' ' && i < strlen(a))
    {
        c++;
        if (length > max)
            max = length;
        //printf("%d\n%d", max, c);    
        word(a, i + 1, max, c);
    }
}

void main()
{
    char a[100];
    int max = 0, j, i = 0, c = 0;
    scanf("%[^\n]s", &a);
    word(a, 0, &max, &c);
    printf("%d\n%d", c, max);
}

给定问题给定一个带空格的字符串,编写一个算法和一个 C 程序来计算其中的单词数和最长单词的长度。例如,如果输入字符串是“我喜欢编程”,那么单词数是 3,最长字符串的长度是 11。

在递归函数word() 中通过引用传递变量maxc 时遇到问题。变量未正确传递。我希望通过引用递归地传递变量,以便在每次递归调用时更新它们的值。我是stackoverflow的新手,如有错误请指正。

【问题讨论】:

  • printf("%d %d", max , c);-->printf("%d %d", *max , *c);
  • 提高编译器的警告级别,重新编译,阅读并理解警告,修复代码,重新开始。

标签: c function recursion pass-by-reference


【解决方案1】:

使用* 意味着您传递的是指针,而不是引用(C 没有引用)。您可能应该阅读一些信息页面,例如 this really clear onethis,以熟悉两者之间的区别。

做例如。然后max = length 更改max 指向的地址,而不是更改该地址的值。您的意思是*max = length,您在其中取消引用指针以获取它指向的地址处的值,然后分配给该值,这会按照您的意图更新“引用”变量。鉴于此,printf("%d %d", max , c) 不会打印值 - 您需要更多 * 的值。

同样,c++ 将指向c 的地址增加一,它不会增加c 指向的整数(为此,您应该使用(*c)++)。


您应该始终使用至少-Wall 进行编译,因此编译器会为您提供完整的警告。在这种情况下,您将收到有关无效指针分配的多个警告,包括它们发生的位置,以便您可以更轻松地修复它们。

【讨论】:

    【解决方案2】:

    我们初学者应该互相帮助。

    你来了。

    #include <stdio.h>
    #include <string.h>
    
    size_t word( const char *s, size_t *max_word )
    {
        size_t count = 0;
    
        s = s + strspn( s, " \t" );
        if ( *s )
        {
            ++count;
    
            size_t n = strcspn( s, " \t" );
    
            if ( *max_word < n ) *max_word = n;
    
            count += word( s + n, max_word );
        }
    
        return count;
    }
    
    int main(void) 
    {
        const char *s = "I like programming using C";
        size_t max_word = 0;
        size_t n = word( s, &max_word );
    
        printf( "Number of words in the string is %zu\n"
                "The length of the maximum word is %zu\n ",
                n, max_word );
    
        return 0;
    }
    

    程序输出是

    Number of words in the string is 5
    The length of the maximum word is 11
    

    至于您的代码,那么对于根据 C 标准的初学者,不带参数的函数 main 应声明为

    int main( void )
    

    函数声明

    void word(char a[100], int i, int *max, int *c);
    

    太复杂了。如果你可以从函数中返回有用的东西,你应该返回它。

    声明数组大小的第一个参数没有太大意义,因为参数被调整为指针。

    由于字符串在函数中没有改变,它应该用限定符const声明。

    第二个参数是多余的,因为包含字符串的字符数组的标记值等于'\0'。至少它应该有size_t的类型。

    请注意符号 '\t' 也可用作单词分隔符。

    初始字符串和任何子字符串都可以以空格开头。然而你的函数忽略了这个事实。

    第一个和第二个 while 循环永远不会执行,在这种情况下,函数返回一个无效的字数和最大长度值。

    还有这个说法

    c++;
    

    没有意义,因为它增加了指针而不是增加了指针指向的值。

    此声明

    max = length;
    

    也会改变指针本身,而不是改变指针所指向的值。

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2018-03-07
      • 2011-05-02
      • 2012-10-10
      • 2020-05-15
      相关资源
      最近更新 更多