【问题标题】:called object 'strn' is not a function [closed]被称为对象'strn'不是函数[关闭]
【发布时间】:2015-05-06 07:41:06
【问题描述】:

在编译下面的代码时,我得到了错误

"被调用的对象 strn 不是函数"

厌倦了这个错误!需要一个解决方案!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num 400
int main()
{
    char strn[num];
    int count;
    int a=0,e=0,i=0,o=0,u=0;
    printf("enter your string!\n");
    gets(strn);
    for(count=0;count<strlen(strn);count++)
    {
        if ( strn(count)=='a' )
        {
            a++;
        }
        if (strn(count)=='e')
        {
            e++;
        }

【问题讨论】:

  • 你需要[]而不是()这里:strn(count)并使用fgets而不是getsgets 很危险,因为它不能防止缓冲区溢出

标签: c arrays function compiler-errors


【解决方案1】:

您正在尝试将strn 用作函数:strn(count)

您可能正在尝试访问count 索引处的值,因此您应该使用strn[count]

【讨论】:

    【解决方案2】:

    这个错误非常具有指示性。您已将 strn 声明为字符数组。

    char strn[num];
    

    并将其用作strn(count),这是错误的。编译器将其视为一个函数。您应该使用方括号 [ ] 而不是括号 ( )

    【讨论】:

      【解决方案3】:

      在您的代码中,strn(count) 表示strn() 的函数调用,带有一个参数count。您需要的是使用Array subscripting 运算符[],而不是()

      你需要改变

      strn(count)
      

      strn[count]
      

      另外,请考虑使用fgets() 而不是gets()

      【讨论】:

        【解决方案4】:

        下标运算符使用符号[] 来包围索引。所以例如而不是

        strn(count)=='a' 
        

        你必须写

        strn[count]=='a' 
        

        C 标准不再支持函数gets,因为它是一个不安全的函数。请改用fgets

        程序可能看起来像

        #include <stdio.h>
        #include <ctype.h>
        
        #define num 400
        
        int main( void )
        {
            char strn[num];
            char *p;
            int a = 0, e = 0, i = 0, o = 0, u = 0;
        
            printf( "Enter your string: " );
            fgets( strn, num, stdin );
        
            for ( p = strn; *p != '\0'; ++p )
            {
                char c = tolower( *p );
        
                switch ( c )
                {
                case 'a':    
                    a++;
                    break;
                case 'e':
                    e++;
                    break;
                // and so on...
        

        【讨论】:

          猜你喜欢
          • 2012-06-22
          • 2022-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-28
          相关资源
          最近更新 更多