From 26417fa190f263b80207d423e53c6081f5f42a41 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Wed, 9 Apr 2014 16:45:57 +0200 Subject: [PATCH] more comments, added a example to do it wrong ;) --- c_pointers/getline.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/c_pointers/getline.c b/c_pointers/getline.c index 2724e51..aef892d 100644 --- a/c_pointers/getline.c +++ b/c_pointers/getline.c @@ -46,6 +46,7 @@ char *trim(char *phrase) { char *old = phrase; char *new = phrase; + // skip blank chars while (*old == ' ') { old++; } @@ -56,14 +57,20 @@ char *trim(char *phrase) { old++; } + // set string end char *new = 0; + return (char*) realloc(phrase, strlen(phrase) + 1); } int main(void) { + // this is wrong -> realloc in trim() on array which wasn't malloc'ed + // char blub[] = " wurstgesicht"; + // printf("%s\n", trim(blub)); + // by using return value of getline(), we got an array which was malloc'ed char *buffer = trim(getLine()); if (buffer == NULL) {