简体   繁体   English

错误表达式必须有指向 object 类型的指针

[英]Error expression must have pointer to object type

#include <stdio.h>
int length(char s)
{ int i;
    for(i=0;s[i]!='\0';i++)
    {}
    printf("%d",i);
}

int main()
{
    char s[100];
    scanf("%s",s);
    length(s);
    

}

I am trying to write a simple program which prints the no.我正在尝试编写一个简单的程序来打印号码。 of characters in a string wihtout using strlen but i am getting these error messages which are beyond my scope of understanding as of now.没有使用 strlen 的字符串中的字符,但我收到这些错误消息超出了我的 scope 到目前为止的理解。

Since you are initiated your function as Integer , you should return any argument in the function length .由于您已将 function 启动为Integer ,因此您应该返回 function length中的任何参数。 But also you print something of length function then you should use function as void但是你也打印一些长度 function 那么你应该使用 function 作为void

#include<stdio.h>

void length(char *s)
{
    int i;
    for(i=0;s[i]!='\0';i++){}
    printf("%d",i);
}

int main()
{
    char s[100];
    scanf("%s",s);
    length(s);
    return 0;

}

Furthermore, You pass just a character array and you run a loop for the character that's why it showing an exception.此外,您只传递一个字符数组,然后为该字符运行一个循环,这就是它显示异常的原因。 If you want you to alive your function as integer then pass a character array like,如果你想让你的 function 像 integer 一样活着,那么传递一个像这样的字符数组,

#include<stdio.h>

int length(char *s)
{
    int i;
    for(i=0;s[i]!='\0';i++){}
    printf("%d",i);
}

int main()
{
    char s[100];
    scanf("%s",s);
    length(s);
    return 0;

}
#include <stdio.h>
int length(char s[])
{ int i;
    for(i=0;s[i]!='\0';i++)
    {}
    printf("%d",i);
}

int main()
{
    char s[100];
    scanf("%s",s);
    length(s);
    

}`

Found my mistake发现我的错误

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

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