c - Why does my code print the whole string multiple times rather than 1 time? -
i'm new c. i'm having trouble understanding fundamental materials in reading input , pointers. want use nextchar() function read , print each character of string enter in command line. try typing "hello"..it displays "hello" 6 times. can tell me why happens? how can fix it? thank time!
#include <stdio.h> #include <assert.h> char nextchar(char* ptr) { static int = -1; char c; ++i; c = *(s+i); if ( c == '\0' ) return '\0'; else return c; } void display(char* ptr) { assert(ptr != 0); { printf("%s", ptr); } while (nextchar(ptr)); } int main(int argc, const char * argv[]) { char* ptr=argv[1]; display(ptr); return 0; }
the nextchar function reduced:
char nextchar(char* ptr) { static int = 0; i++; return (*(ptr+i)); } and display to
void display(char* ptr) { assert(ptr != 0); char c = *ptr; { printf("%c", c); } while (c = nextchar(ptr)); }
Comments
Post a Comment