Browse Source

more comments, added a example to do it wrong ;)

master
T. Meissner 11 years ago
parent
commit
26417fa190
1 changed files with 7 additions and 0 deletions
  1. +7
    -0
      c_pointers/getline.c

+ 7
- 0
c_pointers/getline.c View File

@ -46,6 +46,7 @@ char *trim(char *phrase) {
char *old = phrase; char *old = phrase;
char *new = phrase; char *new = phrase;
// skip blank chars
while (*old == ' ') { while (*old == ' ') {
old++; old++;
} }
@ -56,14 +57,20 @@ char *trim(char *phrase) {
old++; old++;
} }
// set string end char
*new = 0; *new = 0;
return (char*) realloc(phrase, strlen(phrase) + 1); return (char*) realloc(phrase, strlen(phrase) + 1);
} }
int main(void) { 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()); char *buffer = trim(getLine());
if (buffer == NULL) { if (buffer == NULL) {


Loading…
Cancel
Save