{"id":301334,"date":"2024-05-20T18:05:26","date_gmt":"2024-05-20T10:05:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/301334.html"},"modified":"2024-05-20T18:05:32","modified_gmt":"2024-05-20T10:05:32","slug":"c%e8%af%ad%e8%a8%80-%e8%af%a5%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%ae%9a%e4%b9%89%e7%bb%93%e6%9e%9c%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%ba1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/301334.html","title":{"rendered":"C\u8bed\u8a00 \u8be5\u4e8c\u7ef4\u6570\u7ec4\u5b9a\u4e49\u7ed3\u679c\u4e3a\u4ec0\u4e48\u4e3a1"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26213558\/94dfe6d7-7313-46ec-b434-802b7e33ba13.webp\" alt=\"C\u8bed\u8a00 \u8be5\u4e8c\u7ef4\u6570\u7ec4\u5b9a\u4e49\u7ed3\u679c\u4e3a\u4ec0\u4e48\u4e3a1\" \/><\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u5728C\u8bed\u8a00\u4e2d\u5b9e\u8d28\u4e0a\u662f\u6570\u7ec4\u7684\u6570\u7ec4\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0f<code>int arr[2][3]<\/code>\u6765\u8bf4\uff0c\u5176\u5b9a\u4e49\u7684\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e00\u7ef4\u5927\u5c0f\u4e3a2\uff0c\u7b2c\u4e8c\u7ef4\u5927\u5c0f\u4e3a3\u3002<strong>\u5982\u679c\u8003\u8651\u8868\u8fbe\u5f0f<code>sizeof(arr) \/ sizeof(arr[0][0])<\/code>\u7684\u7ed3\u679c\uff0c\u5219\u7ed3\u679c\u662f\u6574\u4e2a\u6570\u7ec4\u5927\u5c0f\u4e0e\u5355\u4e2a\u5143\u7d20\u5927\u5c0f\u7684\u5546\uff0c\u8fd9\u901a\u5e38\u4e0d\u4e3a1\u3002\u82e5\u7ed3\u679c\u4e3a1\uff0c\u5219\u53ef\u80fd\u662f\u5728\u5904\u7406\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u7684\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982<code>sizeof(arr[0]) \/ sizeof(arr[0][0])<\/code>\uff0c\u8be5\u8868\u8fbe\u5f0f\u8ba1\u7b97\u7684\u662f\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7b2c\u4e00\u884c\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e8c\u7ef4\u6570\u7ec4\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5bb9\u6613\u5c06\u4e8c\u7ef4\u6570\u7ec4\u60f3\u8c61\u4e3a\u4e00\u4e2a\u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c<code>int arr[2][3] = {{1,2,3},{4,5,6}};<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a2\u00d73\u7684\u6574\u578b\u4e8c\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7\u6307\u9488\u548c\u6570\u7ec4\u4e0b\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u7ef4\u6570\u7ec4\u5185\u5b58\u5e03\u5c40\u662f\u8fde\u7eed\u7684<\/strong>\uff0c\u5373\u7b2c\u4e8c\u884c\u7684\u9996\u5143\u7d20\u7d27\u968f\u7b2c\u4e00\u884c\u672b\u5143\u7d20\u7684\u540e\u9762\u3002\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u5185\u5b58\u4e2d\u7684\u5e03\u5c40\u987a\u5e8f\u5c06\u662f1,2,3,4,5,6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001sizeof\u51fd\u6570\u4e0e\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>sizeof<\/code>\u662fC\u8bed\u8a00\u4e2d\u7684\u4e00\u79cd\u64cd\u4f5c\u7b26\uff0c\u7528\u4e8e\u8ba1\u7b97\u53d8\u91cf\u6216\u7c7b\u578b\u5728\u5185\u5b58\u4e2d\u7684\u5927\u5c0f\u3002\u5bf9\u4e8e\u6570\u7ec4\u6765\u8bf4\uff0c<code>sizeof(array)<\/code>\u5c06\u8fd4\u56de\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u5360\u7528\u7684\u603b\u5b57\u8282\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4<code>int arr[2][3];<\/code>\uff0c<code>sizeof(arr)<\/code>\u4f1a\u7ed9\u51fa\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u5047\u8bbe<code>int<\/code>\u7c7b\u578b\u5360\u75284\u5b57\u8282\uff0c\u90a3\u4e48<code>sizeof(arr)<\/code>\u7684\u7ed3\u679c\u5c06\u662f2\u00d73\u00d74 = 24\u5b57\u8282\u3002\u800c<code>sizeof(arr[0][0])<\/code>\u5f97\u5230\u7684\u662f\u5355\u4e2a\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u8fd9\u91cc\u5c31\u662f4\u5b57\u8282\u3002\u56e0\u6b64\uff0c<code>sizeof(arr) \/ sizeof(arr[0][0])<\/code>\u7684\u7ed3\u679c\u662f24\/4 = 6\uff0c\u8fd9\u4ee3\u8868\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u603b\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u5982\u679c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a1\uff0c\u90a3\u4e48\u53ef\u80fd\u662f\u56e0\u4e3a\u8868\u8fbe\u5f0f\u5199\u9519\u4e86\u6216\u8005\u5728\u7279\u5b9a\u7684\u8bed\u5883\u4e2d\u7406\u89e3<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e8c\u7ef4\u6570\u7ec4\u7684\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u8981\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u9700\u8981\u4e24\u4e2a\u7d22\u5f15\uff1a\u4e00\u4e2a\u7528\u4e8e\u7b2c\u4e00\u7ef4\uff08\u884c\uff09\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u7b2c\u4e8c\u7ef4\uff08\u5217\uff09\u3002\u4f8b\u5982<code>arr[1][2]<\/code>\u5c06\u8bbf\u95ee\u5230\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u6b63\u786e\u7406\u89e3\u548c\u4f7f\u7528\u7d22\u5f15\u5bf9\u4e8e\u7f16\u7a0b\u4e2d\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u81f3\u5173\u91cd\u8981\u3002\u4e60\u60ef\u4e0a\uff0c\u6211\u4eec\u5c06\u7b2c\u4e00\u4e2a\u7d22\u5f15\u89c6\u4e3a\u884c\u6807\uff0c\u7b2c\u4e8c\u4e2a\u7d22\u5f15\u89c6\u4e3a\u5217\u6807\uff0c\u4f46\u8fd9\u7eaf\u7cb9\u662f\u4e2a\u7ea6\u5b9a\uff0c\u5e76\u4e0d\u5f71\u54cd\u5b9e\u9645\u7684\u5185\u5b58\u5e03\u5c40\u6216\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bef\u89e3\u4e0e\u6392\u9519<\/h3>\n<\/p>\n<p><p>\u770b\u5230\u8868\u8fbe\u5f0f<code>sizeof(arr) \/ sizeof(arr[0][0])<\/code>\u7ed3\u679c\u4e3a1\u7684\u8bf4\u6cd5\uff0c\u6700\u6709\u53ef\u80fd\u7684\u662f\u4eba\u4eec\u5bf9\u5176\u542b\u4e49\u6216\u6240\u5904\u4e0a\u4e0b\u6587\u6709\u8bef\u89e3\u3002\u5982\u679c\u786e\u5b9e\u5728\u4ee3\u7801\u4e2d\u9047\u5230\u4e86\u7c7b\u4f3c\u95ee\u9898\uff0c\u8981\u68c0\u67e5\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u786e\u8ba4\u6570\u7ec4\u5b9a\u4e49\u548c\u4f7f\u7528\u65e0\u8bef<\/strong>\uff1a\u786e\u4fdd\u6570\u7ec4\u58f0\u660e\u5f62\u5f0f\u6b63\u786e\uff0c\u672a\u88ab\u610f\u5916\u5730\u4fee\u6539\u3002<\/li>\n<li><strong>\u7406\u89e3sizeof\u7684\u4f5c\u7528\u8303\u56f4<\/strong>\uff1a\u4e86\u89e3<code>sizeof<\/code>\u662f\u5982\u4f55\u8ba1\u7b97\u7ed9\u5b9a\u7c7b\u578b\u6216\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<li><strong>\u68c0\u67e5\u4e0a\u4e0b\u6587<\/strong>\uff1a\u7406\u89e3\u4ee3\u7801\u7684\u4e0a\u4e0b\u6587\uff0c\u662f\u5426\u6709\u67d0\u4e9b\u7279\u5b9a\u7684\u6761\u4ef6\u4f7f\u5f97\u7ed3\u679c\u4e3a1\uff0c\u6bd4\u5982\u6570\u7ec4\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u7f16\u8bd1\u5668\u4e0e\u5e73\u53f0\u5dee\u5f02<\/strong>\uff1a\u4e0d\u540c\u5e73\u53f0\u548c\u7f16\u8bd1\u5668\u5bf9\u4e8e\u7c7b\u578b\u5927\u5c0f\u7684\u5b9a\u4e49\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u52a1\u5fc5\u786e\u8ba4\u5728\u5f53\u524d\u73af\u5883\u4e0b<code>int<\/code>\u7c7b\u578b\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8fdb\u9636\uff1a\u6307\u9488\u4e0e\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u4e0e\u6307\u9488\u7d27\u5bc6\u76f8\u5173\u3002\u4e8b\u5b9e\u4e0a\uff0c\u6570\u7ec4\u540d\u5728\u5927\u591a\u6570\u4e0a\u4e0b\u6587\u4e2d\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6307\u5411\u5176\u9996\u5143\u7d20\u7684\u6307\u9488\u3002\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u6307\u9488\u548c\u6570\u7ec4\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u548c\u89e3\u51b3\u590d\u6742\u7684\u4e8c\u7ef4\u6570\u7ec4\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>int (*ptr)[3]<\/code>\u58f0\u660e\u4e86\u4e00\u4e2a\u6307\u5411\u5305\u542b3\u4e2a\u6574\u6570\u7684\u4e00\u7ef4\u6570\u7ec4\u7684\u6307\u9488\u3002\u5982\u679c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u4e8c\u7ef4\u6570\u7ec4<code>arr<\/code>\u7684\u7b2c\u4e00\u884c\uff0c\u90a3\u4e48<code>*ptr<\/code>\u4f1a\u8bbf\u95ee\u5230\u6574\u884c\u7684\u5185\u5bb9\uff0c\u800c<code>(*ptr)[2]<\/code>\u5219\u4f1a\u8bbf\u95ee\u5230\u8fd9\u4e2a\u6307\u9488\u5f53\u524d\u6307\u5411\u7684\u884c\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u7406\u89e3\u6307\u9488\u5982\u4f55\u4e0e\u4e8c\u7ef4\u6570\u7ec4\u4ea4\u4e92\uff0c\u5c06\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u9ad8\u6548\u548c\u66f4\u7075\u6d3b\u7684\u4ee3\u7801\uff0c\u5e76\u80fd\u66f4\u597d\u5730\u7406\u89e3C\u8bed\u8a00\u4e2d\u5185\u5b58\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u5e76\u4f7f\u5176\u7ed3\u679c\u4e3a1\uff1f<\/strong><\/p>\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9a\u4e49\u662f\u5728\u58f0\u660e\u65f6\u6307\u5b9a\u6570\u7ec4\u7684\u884c\u548c\u5217\u6570\u3002\u8981\u4f7f\u4e8c\u7ef4\u6570\u7ec4\u7684\u7ed3\u679c\u4e3a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-c\">int array[1][1] = {1};\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8be5\u5143\u7d20\u7684\u503c\u4e3a1\u3002<\/p>\n<p><strong>2. \u4e3a\u4ec0\u4e48\u4e8c\u7ef4\u6570\u7ec4\u5b9a\u4e49\u7ed3\u679c\u4e3a1\u65f6\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e8c\u7ef4\u6570\u7ec4\u7684\u884c\u6570\u548c\u5217\u6570\u90fd\u4e3a1\u3002\u56e0\u6b64\uff0c\u8be5\u4e8c\u7ef4\u6570\u7ec4\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5373array[0][0]\uff0c\u5e76\u4e14\u5176\u503c\u4e3a1\u3002<\/p>\n<p><strong>3. \u9664\u4e86\u5c06\u884c\u548c\u5217\u6570\u90fd\u8bbe\u7f6e\u4e3a1\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4e8c\u7ef4\u6570\u7ec4\u5b9a\u4e49\u7ed3\u679c\u4e3a1\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0c\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u4f7f\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9a\u4e49\u7ed3\u679c\u4e3a1\uff1a<\/p>\n<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n<pre><code class=\"language-c\">int array[1][1];\narray[0][0] = 1;\n<\/code><\/pre>\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<pre><code class=\"language-c\">int array[][1] = {1};\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u5c06\u521b\u5efa\u4e00\u4e2a\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u7684\u503c\u8bbe\u7f6e\u4e3a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e8c\u7ef4\u6570\u7ec4\u5728C\u8bed\u8a00\u4e2d\u5b9e\u8d28\u4e0a\u662f\u6570\u7ec4\u7684\u6570\u7ec4\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0fint arr[2][3]\u6765\u8bf4\uff0c\u5176\u5b9a\u4e49\u7684\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c [&hellip;]","protected":false},"author":3,"featured_media":301340,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/301334"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=301334"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/301334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/301340"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=301334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=301334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=301334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}