c - Pointer / function argument issue? -
i'm making little program use stack implemented dynamic array check if phrase has balanced parenthesis , brackets. far, i've tried passing phrase isbalanced function , try , print out each char 1 one. when program gets to:
printf("%s\n", nextchar(s));
i segmentation fault , warning passing type char , expecting type int.any appreciated.
char nextchar(char* s) { static int = -1; char c; ++i; c = *(s+i); if ( c == '\0' ) return '\0'; else return c; } int isbalanced(char* s) { while(nextchar(s) != 0){ printf("%s\n", nextchar(s)); } return 0; } int main(int argc, char* argv[]) { char* s=argv[1]; int res; res = isbalanced(s); return 0; }
printf
expects char *
, however, code
printf("%s", nextchar(s));
gives char
, because nextchar(s)
returns char
(where must warning, if use compiler).
so, change to,
printf("%c\n", nextchar(s));
also, you're calling nextchar(s)
twice, losing value of first call.
should expecting:
int isbalanced(char* s) { char ch; while((ch = nextchar(s)) != 0){ printf("%c\n", ch); } return 0; }
Comments
Post a Comment