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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -