[prev] [index] [next]

Dynamic Memory Allocation (cont)

Example: write a function lowerCase() to return a lower-cased version of a string

char *old = "This is MY string.";
char *new;

new = lowerCase(old);

print("%s\n%s\n", old, new);
// Output:
// This is MY string.
// this is my string.

Notes:

  • the old string is unchanged, so the function is making a new copy
  • old is a pointer to a string in the global data area
  • new has no string buffer unless it's made to point at one
  • tolower(ch) function in ctype.h is useful here