9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia08/ex16.c (615B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <string.h>
   4 
   5 typedef enum {
   6     OK,
   7     ERROR_MEMORY_ALLOC,
   8     ERROR_NULL_POINTER
   9 } status_t;
  10 
  11 status_t strclone(const char *, char **);
  12 
  13 int main (void) {
  14     char str[] = "Hello, world!";
  15     char *cpy;
  16     status_t st;
  17 
  18     if((st = strclone(str, &cpy))) return st;
  19 
  20     printf("%s\n", cpy);
  21 
  22     free(cpy);
  23     return 0;
  24 }
  25 
  26 status_t strclone(const char *s, char **d)
  27 {
  28     if(!s || !d) return ERROR_NULL_POINTER;
  29 
  30     /* +1 for the null character */
  31     if(!(*d = (char *)calloc(sizeof(char), (strlen(s) + 1)))) return ERROR_MEMORY_ALLOC;
  32 
  33     for(size_t i = 0; *s; s++, i++) (*d)[i] = *s;
  34 
  35     return OK;
  36 }