被称为对象'strn'不是一个函数

时间:2015-05-06 07:41:07

标签: c arrays function compiler-errors

编译以下代码时,我收到错误

  

“被叫对象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++;
        }

4 个答案:

答案 0 :(得分:3)

您正在尝试使用strn,就好像它是一个功能:strn(count)

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

答案 1 :(得分:2)

错误很有说服力。您已将strn声明为字符数组。

char strn[num];

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

答案 2 :(得分:2)

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

您需要更改

strn(count)

strn[count]

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

答案 3 :(得分:2)

下标运算符使用符号[]来封闭索引。例如,而不是

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...