c - Compiler doesn't show any errors or warnings but the program doesn't work -
i tried build , run following program, breaks down executing. thought maybe made mistake 0 errors , 0 warnings shown.
after researching such behavior on stackoverflow, saw misplaced semicolons or forgotten address-operators, not see in source code or overlooking something? c or gcc guru tell me wrong , why?
operating system windows 7, , compiler had enabled: -pedantic -w -wextra -wall -ansi
here source code:
#include <stdio.h> #include <string.h> char *split(char * wort, char c) { int = 0; while (wort[i] != c && wort[i] != '\0') { ++i; } if (wort[i] == c) { wort[i] = '\0'; return &wort[i+1]; } else { return null; } } int main() { char *in = "some text here"; char *rest; rest = split(in,' '); if (rest == null) { printf("\nstring not devided!"); return 1; } printf("\nerster teil: "); puts(in); printf("\nrest: "); puts(rest); return 0; }
the expected behavior string "some text here" split @ first space ' ' , expected output be:
erster teil: rest: text here
you modifying string literal, that's undefined behavior. change this
char* in = "some text here";
to
char in[] = "some text here";
this makes in
array , initializes "some text here"
. should use const
prevent accidentally having bug when define pointer string literal.
Comments
Post a Comment