{"id":5920,"date":"2023-01-04T00:21:18","date_gmt":"2023-01-03T15:21:18","guid":{"rendered":"https:\/\/www.chihayafuru.jp\/tech\/?p=5920"},"modified":"2023-03-22T17:48:34","modified_gmt":"2023-03-22T08:48:34","slug":"c%e8%a8%80%e8%aa%9e-const%e4%bf%ae%e9%a3%be%e5%ad%90%e3%81%97%e3%81%9f%e3%83%9d%e3%82%a4%e3%83%b3%e3%82%bf","status":"publish","type":"post","link":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/archives\/5920","title":{"rendered":"[C\u8a00\u8a9e] const\u4fee\u98fe\u3057\u305f\u30dd\u30a4\u30f3\u30bf"},"content":{"rendered":"<ol>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u304c read-only \u3067\u3042\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c read-only \u3067\u3042\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u3068\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u3068\u3082\u306b read-only \u3067\u3042\u308b\u3002<\/li>\n<ol>\n<!--more--><\/p>\n<h3>0. read-only \u5236\u7d04\u306a\u3057<\/h3>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\n\nint main(void) {\n    int a = 3;\n    int b = 7;\n    int* p;\n    \n    p = &amp;a;\n    printf(\" a = %d\\n\",  a);    \/* 3 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 3 *\/\n\n    p = &amp;b;\n    printf(\" b = %d\\n\",  b);    \/* 7 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 7 *\/\n\n    *p = 9;\n    printf(\" b = %d\\n\",  b);    \/* 9 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 9 *\/\n\n    return 0;\n}<\/pre>\n<ol>\n<li>\u5909\u6570 a \u3068 b \u306f\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u304c\u6307\u3059\u5148\u3092\u5909\u6570 a \u304b\u3089 \u5909\u6570 b \u306b\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u3092\u4ecb\u3057\u3066 \u5909\u6570 a \u3068 b \u306e\u5024\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<\/ol>\n<h3>1. \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u304c read-only \u3067\u3042\u308b<\/h3>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\n\nint main(void) {\n    int a = 3;\n    int b = 7;\n    const int* p;\n    \n    p = &amp;a;\n    printf(\" a = %d\\n\",  a);    \/* 3 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 3 *\/\n\n    p = &amp;b;\n    printf(\" b = %d\\n\",  b);    \/* 7 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 7 *\/\n\n    *p = 9; \/* error: read-only variable is not assignable *\/\n\n    return 0;\n}<\/pre>\n<ol>\n<li>\u5909\u6570 a \u3068 b \u306f\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u304c\u6307\u3059\u5148\u3092\u5909\u6570 a \u304b\u3089 \u5909\u6570 b \u306b\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u3092\u4ecb\u3057\u3066 \u5909\u6570 a \u3068 b \u306e\u5024\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c<font color=\"red\">\u3067\u304d\u306a\u3044<\/font>\u3002<\/li>\n<\/ol>\n<p>\u3061\u306a\u307f\u306b\u6b21\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u3082\u540c\u3058\u3067\u3042\u308b\u3002 \u9593\u63a5\u6f14\u7b97\u5b50 ( <code>*<\/code> ) \u306e\u524d\u306b const\u4fee\u98fe\u5b50 \u3092\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<pre class=\"lang:c decode:true \">int const* p;<\/pre>\n<h3>2. \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c read-only \u3067\u3042\u308b<\/h3>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\n\nint main(void) {\n    int a = 3;\n    int b = 7;\n\n    int* const p = &amp;a;\n    \n    printf(\" a = %d\\n\",  a);    \/* 3 *\/\n    printf(\" b = %d\\n\",  b);    \/* 7 *\/\n\n    printf(\"*p = %d\\n\", *p);    \/* 3 *\/\n\n    *p = 9;\n\n    printf(\"*p = %d\\n\", *p);    \/* 9 *\/\n    printf(\" a = %d\\n\",  a);    \/* 9 *\/\n\n    p = &amp;b; \/* error: cannot assign to variable 'p' with const-qualified type 'int *const' *\/\n\n    return 0;\n}<\/pre>\n<ol>\n<li>\u5909\u6570 a \u3068 b \u306f\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u304c\u6307\u3059\u5148\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c<font color=\"red\">\u3067\u304d\u306a\u3044<\/font>\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u3092\u4ecb\u3057\u3066 \u5909\u6570 a \u306e\u5024\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<\/ol>\n<h3>3. \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u3068\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u3068\u3082\u306b read-only \u3067\u3042\u308b<\/h3>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\n\nint main(void) {\n    int a = 3;\n    int b = 7;\n\n    const int* const p = &amp;a;\n    const int* const q = &amp;b;\n    \n    printf(\" a = %d\\n\",  a);    \/* 3 *\/\n    printf(\"*p = %d\\n\", *p);    \/* 3 *\/\n\n    printf(\" b = %d\\n\",  b);    \/* 7 *\/\n    printf(\"*q = %d\\n\", *q);    \/* 7 *\/\n\n    p = &amp;b; \/* error: cannot assign to variable 'p' with const-qualified type 'int *const' *\/\n\n    *p = 9; \/* error: read-only variable is not assignable *\/\n\n    return 0;\n}<\/pre>\n<ol>\n<li>\u5909\u6570 a \u3068 b \u306f\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u304c\u6307\u3059\u5148\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c<font color=\"red\">\u3067\u304d\u306a\u3044<\/font>\u3002<\/li>\n<li>\u30dd\u30a4\u30f3\u30bf\u5909\u6570 p \u3092\u4ecb\u3057\u3066 \u5909\u6570 \u306e\u5024\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c<font color=\"red\">\u3067\u304d\u306a\u3044<\/font>\u3002<\/li>\n<\/ol>\n<\/ol>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u304c read-only \u3067\u3042\u308b\u3002 \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c read-only \u3067\u3042\u308b\u3002 \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u6307\u3059\u5148\u3068\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c\u3068\u3082\u306b read-only \u3067\u3042\u308b\u3002<\/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-5920","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\/5920","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=5920"}],"version-history":[{"count":15,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/5920\/revisions"}],"predecessor-version":[{"id":6121,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/5920\/revisions\/6121"}],"wp:attachment":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/media?parent=5920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/categories?post=5920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/tags?post=5920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}