Thread: C strings pointer manipulation problem
i have been trying resolve no1 of ruby quiz problems - http://www.rubyquiz.com/quiz1.html in c, program keeps complaining me when try free pointer. please, can me find error? here's code far:
code:#include <string.h> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #define group_length 5 // words in groups of 5 characters char *to_alpha(char *msg); int main(void) { char msg[] = "bla bla"; printf("%s\n", to_alpha(msg)); return exit_success; } int exit_error(char * error) { perror(error); return exit_failure; } /* * uppercases message, removes non-alpha chars. * adds x end of message. */ char *to_alpha(char *msg) { int len_duplicate; char *duplicate; if ((duplicate = malloc(strlen(msg) + 1)) == null) exit_error("null duplicate string"); // pass throught strings once, copy , uppercase // letters [a-za-z] (len_duplicate = 0; *msg; *msg++) if (isalpha(*msg)) { *duplicate++ = toupper(*msg); len_duplicate++; } // append 'x' letters rest of string // until divides group_length in clean manner: if (len_duplicate % group_length != 0) while (len_duplicate % group_length != 0) { *duplicate++ = 'x'; len_duplicate++; } *duplicate++ = '0'; strcpy(msg, duplicate); free(duplicate); return msg; }
the address you're passing free() isn't address got malloc().
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C strings pointer manipulation problem
Ubuntu
Comments
Post a Comment