{"id":2803,"date":"2018-11-23T13:07:43","date_gmt":"2018-11-23T04:07:43","guid":{"rendered":"https:\/\/www.chihayafuru.jp\/tech\/?p=2803"},"modified":"2021-06-01T16:35:39","modified_gmt":"2021-06-01T07:35:39","slug":"c-%e5%8d%b1%e9%99%ba%e3%81%aa%e3%82%ad%e3%83%a3%e3%82%b9%e3%83%88","status":"publish","type":"post","link":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/archives\/2803","title":{"rendered":"[C] \u5371\u967a\u306a\u30ad\u30e3\u30b9\u30c8"},"content":{"rendered":"<p>\u5371\u967a\u306a\u30ad\u30e3\u30b9\u30c8\u3092\u304a\u3053\u306a\u3063\u3066\u30d0\u30b0\u3092\u8a98\u767a\u3057\u305f\u4f8b\u3092\u307f\u3064\u3051\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u4e0d\u5177\u5408\u306e\u7d4c\u7def\u3068\u3057\u3066\u306f<\/p>\n<ol>\n<li>\u3042\u308b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092const\u4fee\u98fe\u5b50\u3092\u3064\u3051\u3066\u5b9a\u7fa9\u3057\u305f\u3002<\/li>\n<li>\u3068\u3053\u308d\u304c\u3053\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u5225\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u306b\u4ee3\u5165\u3059\u308b\u3068\u304d\u306b\u5371\u967a\u306a\u30ad\u30e3\u30b9\u30c8\uff08const\u5916\u3057\uff09\u3092\u304a\u3053\u306a\u3063\u305f\u3002<\/li>\n<li>\u4ee3\u5165\u5148\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u64cd\u4f5c\u3057\u3066const\u4fee\u98fe\u5b50\u3092\u3064\u3051\u305f(read-only\u306e)\u5909\u6570\u306e\u66f8\u304d\u63db\u3048\u3092\u884c\u3063\u305f\u3002<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h3>\u66f8\u8fbc\u307f\u7981\u6b62\u9818\u57df\u306b\u66f8\u8fbc\u307f\u3092\u304a\u3053\u306a\u3063\u3066\u300c\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u9055\u53cd\u300d\u304c\u767a\u751f\u3059\u308b\u4f8b<\/h3>\n<pre class=\"lang:c decode:true \" >#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main(void)\r\n{\r\n  char* hello = \"Hello World\";\r\n\r\n  printf(\"%s\\n\", hello);\r\n\r\n  hello[0] = 'h';    \/* &lt;= \u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\uff08\u30a2\u30af\u30bb\u30b9\uff09\u9055\u53cd *\/\r\n\r\n  printf(\"%s\\n\", hello);\r\n\r\n  return (0);\r\n}<\/pre>\n<p>\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb &#8220;Hello World&#8221; \u306f\u3001read-only\u3067\u3059\u3002Windows\u3084Linux\u306a\u3069\u30e2\u30c0\u30f3\u306a\u30e1\u30e2\u30ea\u4fdd\u8b77\u6a5f\u80fd\u306e\u3042\u308bOS\u74b0\u5883\u4e0b\u3067\u306f\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u9055\u53cd\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<h4>JPCERT \u30bb\u30ad\u30e5\u30a2\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30b9\u30bf\u30f3\u30c0\u30fc\u30c9<\/h4>\n<ul>\n<li>\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u3092\u5909\u66f4\u3057\u306a\u3044 : <a href=\"https:\/\/www.jpcert.or.jp\/sc-rules\/c-str30-c.html\">https:\/\/www.jpcert.or.jp\/sc-rules\/c-str30-c.html<\/a><\/li>\n<li>\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u306e\u53c2\u7167\u306b\u306f const \u3078\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u4f7f\u7528\u3059\u308b : <a href=\"https:\/\/www.jpcert.or.jp\/sc-rules\/c-str05-c.html\">https:\/\/www.jpcert.or.jp\/sc-rules\/c-str05-c.html<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>\u5371\u967a\u306a\u30ad\u30e3\u30b9\u30c8(const\u5916\u3057)\u3092\u304a\u3053\u306a\u3063\u305f\u4f8b<\/h3>\n<pre class=\"lang:c decode:true \" >#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid swap(const char* x, const char* y)\r\n{\r\n  char tmp;\r\n  char* xx = (char*)x;\r\n  char* yy = (char*)y;\r\n\r\n  tmp = *xx;\r\n  *xx = *yy;\r\n  *yy = tmp;\r\n}\r\n\r\nint main(void)\r\n{\r\n  char a = 'A';\r\n  char b = 'B';\r\n\r\n  printf(\"a = %c, b = %c\\n\", a, b);\r\n\r\n  swap(&amp;a, &amp;b);\r\n\r\n  printf(\"a = %c, b = %c\\n\", a, b);\r\n\r\n  return (0);\r\n}<\/pre>\n<p>\u4e0a\u8a18\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u66f8\u304d\u63db\u3048\u306e\u300e\u809d\u300f\u306f<\/p>\n<pre class=\"lang:c decode:true \" >char* xx = (char*)x;<\/pre>\n<p>\u3068\u3057\u3066\u30ad\u30e3\u30b9\u30c8\u6f14\u7b97\u3067 const\u4fee\u98fe\u5b50(read-only\u5236\u7d04)\u3092\u5916\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u30ad\u30e3\u30b9\u30c8\u6f14\u7b97\u3068\u3044\u3046\u3068<\/p>\n<pre class=\"lang:c decode:true \" >char c = \"F\";\r\nint i = (int)c;<\/pre>\n<p>\u306e\u3088\u3046\u306b\u7570\u306a\u308b\u578b\u306e\u5909\u63db\u306b\u4f7f\u308f\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u304c\u3001\u5148\u306e\u4f8b\u306e\u3088\u3046\u306b const\u4fee\u98fe (read-only\u5236\u7d04) \u3092\u5916\u3059\u3053\u3068\u306b\u3082\u3064\u304b\u3048\u307e\u3059\u3002<br \/>\nC\u8a00\u8a9e\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u30fb\u30ec\u30a4\u30e4\uff08\u30e1\u30e2\u30ea\u3084\u30a2\u30c9\u30ec\u30b9\uff09\u306b\u76f4\u63a5\u89e6\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u5207\u308c\u5473\u306e\u92ed\u3044\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3059\u3002\u4e00\u65b9\u3067\u751f\u8eab\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u76f4\u63a5\u64cd\u4f5c\u3067\u304d\u308b\u305f\u3081\u4e0a\u8a18\u306e\u3088\u3046\u306a\u300e\u6a2a\u7d19\u7834\u308a\u300f\u3082\u8a31\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<hr>\n<p>&nbsp;<\/p>\n<h3>\u304a\u307e\u3051<\/h3>\n<p>\u5148\u306b\u4f8b\u3068\u3057\u3066\u3042\u3052\u305fswap\u95a2\u6570\u306e\u95a2\u6570\u5f15\u6570\u306b const\u4fee\u98fe\u5b50 \u3092\u3064\u3051\u308b\u3053\u3068\u306f\u8aac\u660e\u4ee5\u4e0a\u306e\u52b9\u679c\u52b9\u7528\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4e0b\u8a18\u306b\u5b9f\u969b\u306b\u3042\u308a\u5f97\u308b const\u4fee\u98fe\u5b50 \u3092\u5f15\u6570\u306b\u6301\u3064swap\u95a2\u6570\u306e\u4f8b\u3092\u6319\u3052\u307e\u3059\u3002<br \/>\n\u4e0b\u8a18\u306e\u30b1\u30fc\u30b9\u3067\u306f\u6587\u5b57\u5217\u30dd\u30a4\u30f3\u30bf pa \u3068 pb \u304c\u30dd\u30a4\u30f3\u30c8\u3057\u3066\u3044\u308b\u5b9f\u4f53 &#8220;Hello&#8221; \u3068 &#8220;Bye&#8221; \u306f\u66f8\u304d\u63db\u3048\u3066\u3044\u307e\u305b\u3093\u3002&#8221;Hello&#8221; \u3092 &#8220;hello&#8221; \u3084 &#8220;HELLO&#8221; \u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3057\u3001\u5909\u66f4\u3057\u3066\u306f\u306a\u308a\u307e\u305b\u3093\u3002\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u306f\u300e\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb (immutable)\u300f\u3067\u3059\u3002\u6587\u5b57\u5217\u30dd\u30a4\u30f3\u30bf pa \u304c &#8220;Hello&#8221; \u3092\u30dd\u30a4\u30f3\u30c8\u3057\u3066\u3044\u308b\u306e\uff1f &#8220;Bye&#8221; \u3092\u30dd\u30a4\u30f3\u30c8\u3057\u3066\u3044\u308b\u306e\uff1f \u3068\u3044\u3046\u90e8\u5206\u3092 swap \u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:c decode:true \" >#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid swap(const char** x, const char** y)\r\n{\r\n  const char* tmp;\r\n\r\n  tmp = *x;\r\n  *x = *y;\r\n  *y = tmp;\r\n}\r\n\r\nint main(void)\r\n{\r\n  char* pa = \"Hello\";\r\n  char* pb = \"Bye\";\r\n\r\n  printf(\"a = %s, b = %s\\n\", pa, pb);\r\n\r\n  swap((const char**)&amp;pa, (const char**)&amp;pb);\r\n\r\n  printf(\"a = %s, b = %s\\n\", pa, pb);\r\n\r\n  return (0);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5371\u967a\u306a\u30ad\u30e3\u30b9\u30c8\u3092\u304a\u3053\u306a\u3063\u3066\u30d0\u30b0\u3092\u8a98\u767a\u3057\u305f\u4f8b\u3092\u307f\u3064\u3051\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u4e0d\u5177\u5408\u306e\u7d4c\u7def\u3068\u3057\u3066\u306f \u3042\u308b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092const\u4fee\u98fe\u5b50\u3092\u3064\u3051\u3066\u5b9a\u7fa9\u3057\u305f\u3002 \u3068\u3053\u308d\u304c\u3053\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u5225\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u306b\u4ee3\u5165\u3059\u308b\u3068\u304d\u306b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[80],"class_list":["post-2803","post","type-post","status-publish","format-standard","hentry","category-memo","tag-c"],"_links":{"self":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/comments?post=2803"}],"version-history":[{"count":6,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2803\/revisions"}],"predecessor-version":[{"id":2809,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2803\/revisions\/2809"}],"wp:attachment":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/media?parent=2803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/categories?post=2803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/tags?post=2803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}