c - Passing a string and a char to a function -


this code wrote supposed read sentence , character check occurency of character in sentence. works when write code in main, when try use function doesn't work. problem have declaring string , variable char function arguments. wrong?

#include<stdio.h> #include<string.h> int occ(char*,char); int main () {     char c,s[50];     printf("enter sentece:\n");gets(s);     printf("enter letter: ");scanf("%c",&c);     printf("'%c' repeated %d times in sentence.\n",c,occ(s,c)); } int occ(s,c) {     int i=0,j=0;     while(s[i]!='\0')     {         if(s[i]==c)j++;         i++;     }     return j; } 

note should getting warning prototype mismatch between declaration of occ , definition, amongst host of other compilation warnings.

when write:

int occ(s,c) { 

you using pre-standard or k&r style function, , default types of arguments — since didn't specify type — int. that's ok char parameter; not ok char * parameter.

so, else apart, should write:

int occ(char *s, char c) { 

to agree prototype.

when compile code, compilation errors:

$ gcc -o3 -g -std=c99 -wall -wextra -wmissing-prototypes -wstrict-prototypes -wold-style-definition -c wn.c wn.c:4:5: warning: function declaration isn’t prototype [-wstrict-prototypes] wn.c: in function ‘main’: wn.c:4:5: warning: old-style function definition [-wold-style-definition] wn.c: in function ‘occ’: wn.c:11:5: warning: old-style function definition [-wold-style-definition] wn.c:11:5: warning: type of ‘s’ defaults ‘int’ [enabled default] wn.c:11:5: warning: type of ‘c’ defaults ‘int’ [enabled default] wn.c:11:5: error: argument ‘s’ doesn’t match prototype wn.c:3:5: error: prototype declaration wn.c:11:5: error: argument ‘c’ doesn’t match prototype wn.c:3:5: error: prototype declaration wn.c:14:12: error: subscripted value neither array nor pointer nor vector wn.c:16:13: error: subscripted value neither array nor pointer nor vector wn.c:11:5: warning: parameter ‘s’ set not used [-wunused-but-set-parameter] 

note: doesn't take fix code — compiles cleanly. does, however, use fgets() , not gets(). should forget gets() exists now, , teacher should sacked mentioning existence.

example run:

$ ./wn enter sentence: amanaplanacanalpanama enter letter: 'a' repeated 10 times in sentence. $ 

code:

#include <stdio.h>  int occ(char*, char);  int main(void) {     char c, s[50];     printf("enter sentence: ");     fgets(s, sizeof(s), stdin);     printf("enter letter: ");     scanf("%c", &c);     printf("'%c' repeated %d times in sentence.\n", c, occ(s, c));     return 0; }  int occ(char *s, char c) {     int i=0, j=0;     while (s[i]!='\0')     {         if (s[i]==c)             j++;         i++;     }     return j; } 

the code should check both fgets() , scanf() successful; got lazy.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -