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.

135 lines
3.7 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 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. if (headerPtr != NULL) {
  44. strcpy(headerPtr, "Media Player");
  45. printf("%s\n", headerPtr);
  46. *headerPtr = 'W';
  47. *(headerPtr + 1) = 'u';
  48. *(headerPtr + 2) = 'r';
  49. *(headerPtr + 3) = 's';
  50. *(headerPtr + 4) = 't';
  51. *(headerPtr + 5) = '\0';
  52. printf("%s\n", headerPtr);
  53. for (size_t j = 0; j < strlen(headerPtr); j++) {
  54. printf("headerPtr[%zu] Address: %p Value: %c\n", j, &headerPtr[j], headerPtr[j]);
  55. }
  56. printf("\n");
  57. free(headerPtr);
  58. }
  59. // direct init with character pointer
  60. // don't create a new copy, instead a reference to string literal pool only
  61. char *headerRef = "Media Player";
  62. printf("%s\n", headerRef);
  63. // you shouldn't assign a char pointer with a character literal
  64. // because character literals is of type int
  65. // wrong:
  66. // char *prefix = '+';
  67. // better:
  68. char *prefix = malloc(2);
  69. if (prefix != NULL) {
  70. *prefix = '+';
  71. *(prefix + 1) = 0;
  72. printf("%s\n", prefix);
  73. free(prefix);
  74. }
  75. // string initialisation from standard in
  76. // don't forget to allocate memory before read from standard in
  77. // wrong:
  78. // char *command;
  79. // printf("%s: ", "Enter a command");
  80. // scanf("%s", command);
  81. // better:
  82. char *command = malloc(11);
  83. if (command != NULL) {
  84. printf("%s: ", "Enter a command (max. 10 chars): ");
  85. scanf("%10s", command);
  86. printf("You entered: %s with length: %zu\n", command, strlen(command));
  87. free(command);
  88. }
  89. // memset buffer overflow test
  90. // executes without warnings or errors on osx :D
  91. char *mem = malloc(11);
  92. if (mem != NULL) {
  93. memset(mem, 0x42, 16);
  94. for (size_t i = 0; i < 16; i++) {
  95. printf("mem[%zu] Address: %p : %c\n", i, &mem[i], mem[i]);
  96. }
  97. free(mem);
  98. }
  99. }
  100. // standard string operations
  101. // comparing strings
  102. {
  103. char command[16];
  104. printf("Enter a command: ");
  105. scanf("%15s", command);
  106. // return value of strlen == 0 -> strings are equal
  107. if (!strcmp(command, "quit")) {
  108. printf("The command was quit\n");
  109. } else {
  110. printf("The command wasn't quit\n");
  111. }
  112. }
  113. return 0;
  114. }