Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. /*
  2. * const char * const
  3. * : Nothing about it can be changed
  4. *
  5. * const char * == char const *
  6. * : that is a pointer [const char]
  7. * Why same? const char * A; the char value A points to can't be changed
  8. * char const * A; The A pointer
  9. *
  10. * char const *
  11. * : that is constant pointer to a [char]
  12. *
  13. * const int *foo, *bar;
  14. * : both foo, bar are [int const *]
  15. *
  16. * char * const cp == char const * cp
  17. * : the constant variable cp which is the pointer to char.
  18. * : cp's value can't be changed.
  19. *
  20. */
  21.  
  22. /*
  23. * First two const char *p and char const *p both are same i.e. points to constant character
  24. * (You can change where p points, but you can’t change pointed characters using that pointer).
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. int main(int argc, char* argv[])
  30. {
  31. #ifdef DEFAULT
  32. // const char and char const is the same.
  33. const char const * const a; // duplicated!
  34. const char * const b; // Nothing about it can be changed.
  35. const char *c; // the value c points to can't be changed.
  36. char const *d; //
  37. char * const e;
  38. char f = 'f';
  39. char g = 'g';
  40. #endif
  41.  
  42. /* b */
  43. #ifdef B1
  44. char f = 'f';
  45. char g = 'g';
  46. const char * const b = &f; // b has f's mem addr. after that can't be changed
  47. printf("%s\n", b);
  48. b = &g; // Error. can't be assigned mem addr at b.
  49. printf("%s\n", b);
  50. #endif
  51.  
  52. #ifdef B2
  53. char f = 'f';
  54. char g = 'g';
  55. const char * const b = &f; // b has f's mem addr. after that can't be changed
  56. printf("%s\n", b);
  57. *b = "ff"; // Error. can't be changed at the value b points to.
  58. printf("%s\n", b);
  59. #endif
  60.  
  61. /* c */
  62. #ifdef C1
  63. char f = 'f';
  64. char g = 'g';
  65. const char *c = &f; // the value c points to can't be changed.
  66. printf("%s\n", c);
  67. c = &g;
  68. printf("%s\n", c);
  69. #endif
  70.  
  71. #ifdef C2
  72. char f = 'f';
  73. char g = 'g';
  74. const char *c = &f; // the value c points to can't be changed.
  75. printf("%s\n", c);
  76. *c = "ff"; //Error. can't be changed at the value c points to.
  77. printf("%s\n", c);
  78. #endif
  79.  
  80. /* d */
  81. #ifdef D1
  82. char f = 'f';
  83. char g = 'g';
  84. char const *d = &f; // The same with c variable above.
  85. printf("%s\n", d);
  86. d = &g; //Error
  87. printf("%s\n", d);
  88. #endif
  89.  
  90. #ifdef D2
  91. char f = 'f';
  92. char g = 'g';
  93. char const *d = &f; // The same with c variable above.
  94. printf("%s\n", d);
  95. *d = "ff"; //Error.
  96. printf("%s\n", d);
  97. #endif
  98.  
  99. /* e */
  100. #ifdef E1
  101. char f = 'f';
  102. char g = 'g';
  103. char * const e = &f; // var e can't be assigned another mem addr which is char.
  104. printf("%s\n", e);
  105. e = &g; //Error.
  106. printf("%s\n", e);
  107. #endif
  108.  
  109. #ifdef E2
  110. char f = 'f';
  111. char g = 'g';
  112. char * const e = &f; // var e can't be assigned another mem addr which is char.
  113. printf("%s\n", e);
  114. *e = 'z';
  115. printf("%s\n", e);
  116. #endif
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement