{"id":305243,"date":"2024-05-20T19:41:18","date_gmt":"2024-05-20T11:41:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/305243.html"},"modified":"2024-05-20T19:41:39","modified_gmt":"2024-05-20T11:41:39","slug":"c-%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e4%b8%ad-int-a%e5%92%8cint-a%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/305243.html","title":{"rendered":"c  \u51fd\u6570\u53c2\u6570\u4e2d int* &amp;a\u548cint&amp; *a\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221558\/e8fc85f8-1a8e-49ed-bbad-3b1cfa9ffea9.webp\" alt=\"c  \u51fd\u6570\u53c2\u6570\u4e2d int* &amp;a\u548cint&amp; *a\u6709\u4ec0\u4e48\u533a\u522b\" \/><\/p>\n<p><p>\u4f20\u9012\u7ed9C\u8bed\u8a00\u51fd\u6570\u7684\u53c2\u6570<code>int* &amp;a<\/code>\u548c<code>int&amp; *a<\/code>\u5c55\u73b0\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u548c\u5f15\u7528\u65b9\u5f0f\u3002<strong><code>int* &amp;a<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6307\u5411\u6574\u578b\u7684\u6307\u9488\u7684\u5f15\u7528<\/strong>\u3001\u800c<code>int&amp; *a<\/code>\u5219\u662f\u4e00\u4e2a\u4e0d\u5408\u6cd5\u7684\u7c7b\u578b\u7ec4\u5408\uff0c\u56e0\u4e3a\u5728C\u8bed\u8a00\u6807\u51c6\u4e2d\u5f15\u7528\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u8bed\u6cd5\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u5728C++\u4e2d\uff0c<strong><code>int* &amp;a<\/code>\u662f\u5bf9\u6574\u578b\u6307\u9488\u7684\u5f15\u7528<\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u6539\u53d8\u4e86\u6307\u9488\u7684\u503c\uff08\u5373\u5b83\u6307\u5411\u7684\u5730\u5740\uff09\uff0c\u8fd9\u4e9b\u53d8\u5316\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u5916\u90e8\u7684\u90a3\u4e2a\u6307\u9488\u3002\u800c<code>int&amp; *a<\/code>\u5728\u8bed\u6cd5\u4e0a\u662f\u4e0d\u5408\u7406\u7684\uff1b\u6b63\u786e\u7684\u5e94\u8be5\u662f<code>int *&amp;a<\/code>\uff0c<strong>\u8868\u793a\u4e00\u4e2a\u6307\u9488\u6307\u5411\u7684\u662f\u4e00\u4e2a\u6574\u6570\u7684\u5f15\u7528<\/strong>\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u7c7b\u578b\u58f0\u660e\u5728C++\u4e2d\u4e5f\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u56e0\u4e3a\u4e0d\u80fd\u521b\u5efa\u6307\u5411\u5f15\u7528\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<p><p>\u91cd\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5bf9\u6307\u9488\u7684\u5f15\u7528\uff0c\u4f46\u4e0d\u80fd\u521b\u5efa\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5f15\u7528\uff0c\u56e0\u4e3a\u5f15\u7528\u5728C++\u4e2d\u5e76\u4e0d\u662f\u4e00\u4e2a\u53ef\u4ee5\u72ec\u7acb\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u800c\u662f\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u5bf9\u8c61\u7684\u53e6\u4e00\u4e2a\u540d\u5b57\u6216\u522b\u540d\u3002<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5bf9\u6307\u9488\u7684\u5f15\u7528\u7684\u7528\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bf9\u6307\u9488\u7684\u5f15\u7528\uff08<code>int* &amp;a<\/code>\uff09<\/h3>\n<\/p>\n<p><h4>\u53c2\u6570\u4f20\u9012<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u53c2\u6570\u4e2d\u4f7f\u7528<code>int* &amp;a<\/code>\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u51fd\u6570\u4f53\u5185\u4fee\u6539\u5916\u90e8\u4f20\u9012\u8fdb\u6765\u7684\u6307\u9488\u7684\u6307\u5411\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">void modifyPointer(int* &amp;ptr) {<\/p>\n<p>    \/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58<\/p>\n<p>    ptr = new int(10);<\/p>\n<p>}<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    int* p = nullptr;<\/p>\n<p>    modifyPointer(p);<\/p>\n<p>    std::cout &lt;&lt; *p &lt;&lt; std::endl; \/\/ \u8f93\u51fa 10<\/p>\n<p>    delete p; \/\/ \u91ca\u653e\u5185\u5b58<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>modifyPointer<\/code>\u63a5\u53d7\u4e00\u4e2a\u5bf9\u6574\u578b\u6307\u9488\u7684\u5f15\u7528\uff0c\u4e0d\u4ec5\u4fee\u6539\u4e86\u6307\u9488\u6240\u6307\u7684\u5185\u5bb9\uff0c\u8fd8\u6539\u53d8\u4e86\u6307\u9488\u672c\u8eab\u7684\u503c\uff0c\u5373\u5b83\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u51fd\u6570\u8fd4\u56de<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>int* &amp;a<\/code>\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u786e\u4fdd\u80fd\u591f\u5b89\u5168\u5730\u8fd4\u56de\u51fd\u6570\u5185\u90e8\u7684\u6307\u9488\u53d8\u91cf\u6216\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6307\u9488\u6210\u5458\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9519\u8bef\u7684\u7c7b\u578b\u58f0\u660e\uff08<code>int&amp; *a<\/code>\uff09<\/h3>\n<\/p>\n<p><p>\u5728C++\u4e2d\u4f7f\u7528<code>int&amp; *a<\/code>\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u539f\u56e0\u5982\u4e0a\u6240\u8ff0\uff0c\u56e0\u4e3a\u4e0d\u80fd\u521b\u5efa\u6307\u5411\u5f15\u7528\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h4>\u9519\u8bef\u7684\u8bed\u6cd5\u7406\u89e3<\/h4>\n<\/p>\n<p><p>\u5f53\u5f00\u53d1\u8005\u5c1d\u8bd5\u4f7f\u7528<code>int&amp; *a<\/code>\u8fd9\u6837\u7684\u58f0\u660e\u65f6\uff0c\u5f88\u53ef\u80fd\u662f\u5bf9\u6307\u9488\u548c\u5f15\u7528\u7684\u6982\u5ff5\u7406\u89e3\u6709\u8bef\u3002\u8fd9\u91cc\u7684\u4e00\u4e2a\u5e38\u89c1\u8bef\u533a\u662f\u8bd5\u56fe\u58f0\u660e\u4e00\u4e2a\u6307\u9488\uff0c\u5b83\u6307\u5411\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f46\u5728C++\u4e2d\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u6b63\u786e\u7684\u5f15\u7528\u548c\u6307\u9488\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u6b63\u786e\u5730\u4f7f\u7528\u5f15\u7528\u548c\u6307\u9488\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6307\u5411\u6307\u9488\u7684\u5f15\u7528\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u5e94\u5f53\u76f4\u63a5\u4f7f\u7528<code>int&amp; a<\/code>\u6765\u58f0\u660e\u5f15\u7528\u7c7b\u578b\uff0c\u7528\u4ee5\u63d0\u4f9b\u522b\u540d\u6216\u539f\u59cb\u7c7b\u578b\u7684\u76f4\u63a5\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230<code>int* &amp;a<\/code>\u548c<code>int&amp; *a<\/code>\uff08\u5b9e\u9645\u5e94\u4e3a<code>int *&amp;a<\/code>\uff0c\u4e14\u4f9d\u7136\u4e0d\u5408\u6cd5\uff09\u7684\u4e0d\u540c\u4e4b\u5904\u4e3b\u8981\u5728\u4e8e\u5b83\u4eec\u7684\u58f0\u660e\u65b9\u5f0f\u548c\u4f7f\u7528\u4e0a\u7684\u8bed\u6cd5\u6b63\u786e\u6027\u3002\u5728C++\u7f16\u7a0b\u4e2d\uff0c\u6df1\u5165\u7406\u89e3\u5404\u79cd\u7c7b\u578b\u58f0\u660e\u7684\u7ec6\u5fae\u5dee\u522b\u662f\u5f00\u53d1\u5b89\u5168\u3001\u9ad8\u6548\u4ee3\u7801\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f\u6307\u9488\u5f15\u7528\uff08int* &amp;a\uff09\u548c\u5f15\u7528\u6307\u9488\uff08int&amp; *a\uff09\uff1f<\/strong><\/p>\n<p>\u6307\u9488\u5f15\u7528\uff08int* &amp;a\uff09\u662f\u6307\u4e00\u4e2a\u6307\u9488\u7684\u5f15\u7528\uff0c\u5f15\u7528\u6307\u9488\uff08int&amp; *a\uff09\u662f\u6307\u4e00\u4e2a\u5f15\u7528\u7684\u6307\u9488\u3002\u5b83\u4eec\u90fd\u662fC\u8bed\u8a00\u4e2d\u51fd\u6570\u53c2\u6570\u7684\u5f62\u5f0f\uff0c\u7528\u4e8e\u4f20\u9012\u6307\u9488\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p><strong>2. int* &amp;a\u548cint&amp; *a\u5728\u4f7f\u7528\u4e2d\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u4e2d\uff0cint* &amp;a\u8868\u793a\u5c06\u6307\u9488\u7684\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u7684\u503c\uff0c\u5e76\u4e14\u8fd9\u4e2a\u4fee\u6539\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u5916\u90e8\u6307\u9488\u7684\u503c\u3002\u800cint&amp; *a\u8868\u793a\u5c06\u5f15\u7528\u7684\u6307\u9488\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u6240\u6307\u5411\u7684\u503c\uff0c\u5e76\u4e14\u8fd9\u4e2a\u4fee\u6539\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u5916\u90e8\u6240\u6307\u5411\u7684\u503c\u3002<\/p>\n<p><strong>3. \u4ec0\u4e48\u65f6\u5019\u4f7f\u7528int* &amp;a\u548cint&amp; *a\uff1f<\/strong><\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528int* &amp;a\u6765\u4f20\u9012\u6307\u9488\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u7684\u503c\u3002\u8fd9\u5728\u9700\u8981\u52a8\u6001\u5206\u914d\u5185\u5b58\u6216\u5728\u51fd\u6570\u4e2d\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002\u800cint&amp; *a\u5219\u9002\u7528\u4e8e\u9700\u8981\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u6307\u9488\u6240\u6307\u5411\u7684\u503c\uff0c\u4f8b\u5982\u5bf9\u6307\u9488\u6240\u6307\u5411\u7684\u53d8\u91cf\u8fdb\u884c\u4ea4\u6362\u64cd\u4f5c\u7b49\u3002\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5f62\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f20\u9012\u7ed9C\u8bed\u8a00\u51fd\u6570\u7684\u53c2\u6570int* &amp;a\u548cint&amp; *a\u5c55\u73b0\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u548c\u5f15\u7528\u65b9\u5f0f\u3002int [&hellip;]","protected":false},"author":3,"featured_media":305272,"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\/305243"}],"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=305243"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/305272"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=305243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=305243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=305243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}