Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

10 years ago
10 years ago
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. // summary ofstring placement
  5. /*
  6. char *globalHeader = "Chapter";
  7. char globalArrayHeader[] = "Chapter";
  8. void displayHeader() {
  9. static char *staticHeader = "Chapter";
  10. char *localHeader = "Chapter";
  11. static char staticArrayHeader[] = "Chapter";
  12. char localArrayHeader[] = "Chapter";
  13. char *heapHeader = malloc(strlen("Chapter")+1);
  14. strcpy(heapHeader, "Chapter");
  15. }
  16. */
  17. int main(void) {
  18. // print size of char & character literal
  19. printf("%zu\n", sizeof(char));
  20. printf("%zu\n", sizeof('a'));
  21. // when a string is not a constant
  22. // doesn't work with gcc anymore, also with -Wno-deprecated-writable-strings
  23. // gives 'Bus error: 10'
  24. {
  25. char *tabHeader = "Sound";
  26. //*tabHeader = 'L';
  27. printf("%s\n", tabHeader);
  28. }
  29. // string initialization
  30. {
  31. // array of char
  32. char header[] = "Media Player";
  33. printf("%s\n", header);
  34. header[0] = 'W';
  35. header[1] = 'u';
  36. header[2] = 'r';
  37. header[3] = 's';
  38. header[4] = 't';
  39. header[5] = '\0';
  40. printf("%s\n", header);
  41. // pointer to char (with memory allocation)
  42. char *headerPtr = malloc(strlen("Media Player") + 1);
  43. strcpy(headerPtr, "Media Player");
  44. printf("%s\n", headerPtr);
  45. *headerPtr = 'W';
  46. *(headerPtr + 1) = 'u';
  47. *(headerPtr + 2) = 'r';
  48. *(headerPtr + 3) = 's';
  49. *(headerPtr + 4) = 't';
  50. *(headerPtr + 5) = '\0';
  51. printf("%s\n", headerPtr);
  52. for (size_t j = 0; j < strlen(headerPtr); j++) {
  53. printf("headerPtr[%zu] Address: %p Value: %c\n", j, &headerPtr[j], headerPtr[j]);
  54. }
  55. printf("\n");
  56. // direct init with character pointer
  57. // don't create a new copy, instead a reference to string literal pool only
  58. char *headerRef = "Media Player";
  59. printf("%s\n", headerRef);
  60. // you shouldn't assign a char pointer with a character literal
  61. // because character literals is of type int
  62. // wrong:
  63. // char *prefix = '+';
  64. // better:
  65. char *prefix = malloc(2);
  66. *prefix = '+';
  67. *(prefix + 1) = 0;
  68. printf("%s\n", prefix);
  69. free(prefix);
  70. // string initialisation from standard in
  71. // don't forget to allocate memory before read from standard in
  72. // wrong:
  73. // char *command;
  74. // printf("%s: ", "Enter a command");
  75. // scanf("%s", command);
  76. // better:
  77. char *command = malloc(11);
  78. printf("%s: ", "Enter a command");
  79. scanf("%10s", command);
  80. printf("You entered: %s with length: %zu\n", command, strlen(command));
  81. free(command);
  82. // memset buffer overflow test
  83. // executes without warnings or errors on osx :D
  84. char *mem = malloc(11);
  85. memset(mem, 0x42, 64);
  86. free(mem);
  87. }
  88. // standard string operations
  89. // comparing strings
  90. {
  91. char command[16];
  92. printf("Enter a command: ");
  93. scanf("%15s", command);
  94. // return value of strlen == 0 -> strings are equal
  95. if (!strcmp(command, "quit")) {
  96. printf("The command was quit\n");
  97. } else {
  98. printf("The command wasn't quit\n");
  99. }
  100. }
  101. return 0;
  102. }