Reputation: 377
What's wrong with this?
#include <stdio.h>
void main(){
char *s="some text";
printf("%d",is_in(s,'t'));
}
int is_in(char *s, char c){
while(*s){
if(*s==c) return 1;
s++;
}
return 0;
}
I get the following compile time error with GCC:
test.c:9: error: conflicting types for ‘is_in’
test.c:9: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
test.c:5: note: previous implicit declaration of ‘is_in’ was here
Upvotes: 1
Views: 264
Reputation: 95639
You are incrementing the character, not the pointer. Change *s++ to simply s++. In addition, you have forgotten to forward-declare your function "is_in". One other note: you should probably make your string a "const char*" instead of "char*", and, IMHO, explicit comparison with '\0' and using indexes is clearer:
#include <stdio.h>
int is_in(const char*, char);
int main(int argc, char* argv[]){
const char* str="some text";
printf("%d",is_in(s,'t'));
return 0;
}
int is_in(const char* str, char c){
int idx=0;
while ( str[idx] != '\0' ){
if ( str[idx] == c ){
return 1;
}
idx++;
}
return 0;
}
Upvotes: 1