Guest User

Untitled

a guest
Dec 14th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NO_ERROR 0
  6. #define NULL_PTR_ERROR -1
  7.  
  8. typedef char * String;
  9.  
  10. String stringConstructor(int length) {
  11. String newString = (String) malloc(sizeof(char) * length + 1);
  12. newString[length] = 0; //null terminate just in case
  13. return newString;
  14. }
  15.  
  16. int stringSet(String this, char * string) {
  17. if (this==NULL) return NULL_PTR_ERROR;
  18. strcpy(this, string);
  19. return NO_ERROR;
  20. }
  21.  
  22. int main(void) {
  23.  
  24. int error_code =0;
  25.  
  26. String a_string = stringConstructor(64);
  27. error_code = stringSet(a_string, "Hello world");
  28. if (error_code) {
  29. printf("Error - not a valid pointer\n");
  30. }
  31. else printf("%s\n", a_string);
  32. }
Add Comment
Please, Sign In to add comment