{"id":362,"date":"2026-01-04T12:19:30","date_gmt":"2026-01-04T04:19:30","guid":{"rendered":"http:\/\/4.189.252.43\/?p=362"},"modified":"2026-01-04T12:19:31","modified_gmt":"2026-01-04T04:19:31","slug":"%e7%bb%93%e6%9e%84%e4%bd%93","status":"publish","type":"post","link":"http:\/\/4.189.252.43\/index.php\/2026\/01\/04\/%e7%bb%93%e6%9e%84%e4%bd%93\/","title":{"rendered":"\u7ed3\u6784\u4f53"},"content":{"rendered":"\n<h2>1. \u7ed3\u6784\u4f53\u7684\u6982\u5ff5<\/h2>\n<p><strong>\u7ed3\u6784\u4f53\uff08<\/strong>struct\uff09\u7528\u4e8e\u5c06\u591a\u4e2a<strong>\u4e0d\u540c\u7c7b\u578b\u7684\u6210\u5458\u7ec4\u5408\u6210\u4e00\u4e2a\u6574\u4f53<\/strong>\uff0c\u4ee5\u63cf\u8ff0\u590d\u6742\u6570\u636e\u5b9e\u4f53\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code>struct Student {\n    int id;\n    char name[20];\n    float score;\n};\n<\/code><\/pre>\n<p>\u7ed3\u6784\u4f53\u672c\u8d28\u4e0a\u662f\u201c\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u201d\u3002<\/p>\n<hr \/>\n<h2>2. \u7ed3\u6784\u4f53\u5b9a\u4e49\u4e0e\u58f0\u660e<\/h2>\n<h3>2.1 \u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49<\/h3>\n<pre><code>struct Point {\n    int x;\n    int y;\n};\n<\/code><\/pre>\n<h3>2.2 \u58f0\u660e\u7ed3\u6784\u4f53\u53d8\u91cf<\/h3>\n<pre><code>struct Point p;\n<\/code><\/pre>\n<h3>2.3 \u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff08\u63a8\u8350\uff09<\/h3>\n<p>\u4f7f\u7528 <code>typedef<\/code> \u7b80\u5316\u4e66\u5199\uff1a<\/p>\n<pre><code class='language-cpp' lang='cpp'>typedef struct {\n\t\/\/ \u7ed3\u6784\u4f53\u6210\u5458\n    int x;\n    int y;\n} Point;\n\nPoint p;\n<\/code><\/pre>\n<hr \/>\n<h2>3. \u7ed3\u6784\u4f53\u7684\u521d\u59cb\u5316<\/h2>\n<h3>3.1 \u987a\u5e8f\u521d\u59cb\u5316<\/h3>\n<pre><code>struct Point p = {10, 20};\n<\/code><\/pre>\n<h3>3.2 \u6307\u5b9a\u6210\u5458\u521d\u59cb\u5316\uff08C99\uff09<\/h3>\n<pre><code>struct Point p = {.y = 20, .x = 10};\n<\/code><\/pre>\n<hr \/>\n<h2>4. \u7ed3\u6784\u4f53\u6210\u5458\u8bbf\u95ee<\/h2>\n<h3>4.1 \u666e\u901a\u53d8\u91cf\u901a\u8fc7 <code>.<\/code><\/h3>\n<pre><code>p.x = 5;\np.y = 8;\n<\/code><\/pre>\n<h3>4.2 \u6307\u9488\u53d8\u91cf\u901a\u8fc7 <code>-&gt;<\/code><\/h3>\n<pre><code>struct Point *pt = &amp;p;\npt-&gt;x = 5;\npt-&gt;y = 8;\n<\/code><\/pre>\n<p><code>.<\/code> \u548c <code>-&gt;<\/code> \u7684\u5173\u7cfb\uff1a<\/p>\n<pre><code>(*pt).x \u7b49\u4ef7\u4e8e pt-&gt;x\n(*pt).y \u7b49\u4ef7\u4e8e pt-&gt;y\n<\/code><\/pre>\n<hr \/>\n<h2>5. \u7ed3\u6784\u4f53\u4e0e\u5185\u5b58\u5e03\u5c40<\/h2>\n<p>\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u7531\u4ee5\u4e0b\u56e0\u7d20\u5f71\u54cd\uff1a<\/p>\n<ul>\n<li>\u6210\u5458\u987a\u5e8f<\/li>\n<li>\u6210\u5458\u5bf9\u9f50\u8981\u6c42<\/li>\n<li>\u7f16\u8bd1\u5668\u586b\u5145\uff08padding\uff09<\/li>\n\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code>struct A {\n    char c; \/\/ 1\u4e2a\u5b57\u8282\n    char d; \/\/ 1\u4e2a\u5b57\u8282\n    int  i; \/\/ 4\u4e2a\u5b57\u8282\n};\n<\/code><\/pre>\n<p>\u53ef\u80fd\u5927\u5c0f\u4e3a <strong>8 \u5b57\u8282<\/strong>\uff08\u53d6\u51b3\u4e8e\u5e73\u53f0\uff09\uff1a<\/p>\n<ul>\n<li>char \u5360 1 \u5b57\u8282<\/li>\n<li>\u540e\u9762\u8865 3 \u5b57\u8282\u586b\u5145<\/li>\n<li>int \u5360 4 \u5b57\u8282<\/li>\n\n<\/ul>\n<p>\u5927\u5c0f\u7531\uff1a<code>sizeof(struct A)<\/code> \u51b3\u5b9a\u3002<\/p>\n<hr \/>\n<h2>6. \u7ed3\u6784\u4f53\u4e0e\u6307\u9488<\/h2>\n<h3>6.1 \u7ed3\u6784\u4f53\u6307\u9488<\/h3>\n<pre><code>struct Student s;\nstruct Student *p = &amp;s;\n\np-&gt;score = 99.5;\n<\/code><\/pre>\n<h3>6.2 \u6307\u5411\u7ed3\u6784\u4f53\u6210\u5458\u7684\u6307\u9488<\/h3>\n<pre><code>int *ptr = &amp;(p-&gt;id);\n<\/code><\/pre>\n<h3>6.3 \u4f7f\u7528 <code>malloc<\/code> \u52a8\u6001\u521b\u5efa\u7ed3\u6784\u4f53<\/h3>\n<pre><code class='language-cpp' lang='cpp'>struct Student *stu = malloc(sizeof(struct Student));\nstu-&gt;id = 1;\n<\/code><\/pre>\n<p>\u91ca\u653e\uff1a<\/p>\n<pre><code>free(stu);\n<\/code><\/pre>\n<hr \/>\n<h2>7. \u7ed3\u6784\u4f53\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570<\/h2>\n<h3>7.1 \u6309\u503c\u4f20\u9012\uff08\u4f1a\u590d\u5236\u7ed3\u6784\u4f53\uff09<\/h3>\n<pre><code>void print(struct Point p);\n<\/code><\/pre>\n<p>\u9002\u7528\u4e8e\u5c0f\u7ed3\u6784\u4f53\u3002<\/p>\n<h3>7.2 \u6309\u6307\u9488\u4f20\u9012\uff08\u65e0\u590d\u5236\uff0c\u6548\u7387\u9ad8\uff09<\/h3>\n<pre><code>void modify(struct Point *p);\n<\/code><\/pre>\n<hr \/>\n<h2>8. \u7ed3\u6784\u4f53\u5d4c\u5957<\/h2>\n<h3>8.1 \u7ed3\u6784\u4f53\u4f5c\u4e3a\u6210\u5458<\/h3>\n<pre><code class='language-cpp' lang='cpp'>struct Date {\n    int y, m, d;\n}; \/\/ \u7ed3\u6784\u4f53\u7684\u58f0\u660e\u548c\u5b9a\u4e49\n \nstruct Student {\n    int id;\n    struct Date birth;   \/\/ \u7ed3\u6784\u4f53\u7684\u8c03\u7528\n};\n<\/code><\/pre>\n<p>\u8bbf\u95ee\uff1a<\/p>\n<pre><code>s.birth.y = 2000;\n<\/code><\/pre>\n<h3>8.2 \u81ea\u5f15\u7528\u7ed3\u6784\u4f53\uff08\u94fe\u8868\u6838\u5fc3\uff09<\/h3>\n<p>\u5fc5\u987b\u4f7f\u7528\u6307\u9488\uff1a<\/p>\n<pre><code>struct Node {\n    int data;\n    struct Node *next;\n};\n<\/code><\/pre>\n<hr \/>\n<h2>9. \u7ed3\u6784\u4f53\u6570\u7ec4<\/h2>\n<pre><code class='language-cpp' lang='cpp'>struct Point{\n\tint x;\n\tint y;\n}\n\nstruct Point arr[3] = {\n    {1,2},\n    {3,4},\n    {5,6}\n};\n<\/code><\/pre>\n<p>\u8bbf\u95ee\uff1a<\/p>\n<pre><code>arr[1].x = 10;\n<\/code><\/pre>\n<hr \/>\n<h2>10. \u4f4d\u6bb5\uff08Bit-fields\uff09<\/h2>\n<p>\u7528\u4e8e\u7cbe\u786e\u6307\u5b9a\u5b57\u6bb5\u7684\u4f4d\u6570\uff1a<\/p>\n<pre><code>struct Flags {\n    unsigned int a : 1;\n    unsigned int b : 3;\n    unsigned int c : 4;\n};\n<\/code><\/pre>\n<p>\u5e38\u7528\u4e8e\u5d4c\u5165\u5f0f\u4e0e\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<hr \/>\n<h2>11. \u7ed3\u6784\u4f53\u4e0e <code>typedef<\/code> \u7684\u5e38\u89c1\u7ec4\u5408\u6a21\u5f0f<\/h2>\n<h3>11.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<pre><code>typedef struct {\n    int x;\n    int y;\n} Point;\n<\/code><\/pre>\n<h3>11.2 \u547d\u540d\u7ed3\u6784\u4f53 + typedef<\/h3>\n<pre><code>typedef struct Student {\n    int id;\n    int age;\n} Student;\n<\/code><\/pre>\n<h3>11.3 \u81ea\u5f15\u7528\u94fe\u8868\u5e38\u89c1\u5f62\u5f0f<\/h3>\n<pre><code>typedef struct Node {\n    int data;\n    struct Node *next;\n} Node;\n<\/code><\/pre>\n<hr \/>\n<h2>12. \u7ed3\u6784\u4f53\u5185\u5b58\u62f7\u8d1d<\/h2>\n<p>\u7ed3\u6784\u4f53\u53ef\u4ee5\u6574\u4f53\u8d4b\u503c\uff1a<\/p>\n<pre><code>struct Point p1 = {1,2};\nstruct Point p2 = p1;  \/\/ \u6574\u4f53\u62f7\u8d1d\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>memcpy<\/code>\uff1amemory copy<\/p>\n<pre><code>memcpy(&amp;p2, &amp;p1, sizeof(p1));\n<\/code><\/pre>\n<hr \/>\n<h2>13. \u7ed3\u6784\u4f53\u4e0e\u5bf9\u9f50\u63a7\u5236\uff08\u53ef\u9009\u8fdb\u9636\uff09<\/h2>\n<pre><code>#pragma pack(1)\nstruct A {\n    char c;\n    int  i;\n};\n#pragma pack()\n<\/code><\/pre>\n<p>\u5f3a\u5236\u6309 1 \u5b57\u8282\u5bf9\u9f50\uff0c\u53ef\u7528\u4e8e\u901a\u4fe1\u534f\u8bae\u6216\u78c1\u76d8\u683c\u5f0f\u89e3\u6790\u3002<\/p>\n<h1>14. \u679a\u4e3e\uff08enum\uff09<\/h1>\n<p>\u679a\u4e3e\u7528\u4e8e\u5b9a\u4e49\u4e00\u7ec4\u201c\u6709\u610f\u4e49\u7684\u6574\u578b\u5e38\u91cf\u201d\uff0c\u4f7f\u4ee3\u7801\u66f4\u53ef\u8bfb\u3002<\/p>\n<h2>14.1 \u57fa\u672c\u5b9a\u4e49<\/h2>\n<pre><code>enum Color {\n    RED,\n    GREEN,\n    BLUE\n};\n<\/code><\/pre>\n<p>\u679a\u4e3e\u503c\u9ed8\u8ba4\u4ece 0 \u5f00\u59cb\u9012\u589e\uff1a<\/p>\n<pre><code>RED=0, GREEN=1, BLUE=2\n<\/code><\/pre>\n<h2>14.2 \u6307\u5b9a\u679a\u4e3e\u503c<\/h2>\n<pre><code>enum Status {\n    OK = 200,\n    NOT_FOUND = 404,\n    ERROR = 500\n};\n<\/code><\/pre>\n<h2>14.3 typedef \u7b80\u5316<\/h2>\n<pre><code>typedef enum {\n    MON, TUE, WED, THU, FRI, SAT, SUN\n} Weekday;\n\nWeekday w = MON;\n<\/code><\/pre>\n<h2>14.4 \u5e94\u7528\u573a\u666f<\/h2>\n<ul>\n<li>\u72b6\u6001\u7801<\/li>\n<li>\u6709\u9650\u72b6\u6001\u673a<\/li>\n<li>\u6807\u5fd7\u503c\u96c6\u5408<\/li>\n<li>\u9009\u9879\u7c7b\u578b\u5b9a\u4e49<\/li>\n\n<\/ul>\n<hr \/>\n<h1>15. \u8054\u5408\u4f53\uff08union\uff09<\/h1>\n<p>\u8054\u5408\u4f53\u7528\u4e8e\u591a\u4e2a\u6210\u5458 <strong>\u5171\u4eab\u540c\u4e00\u6bb5\u5185\u5b58<\/strong>\u3002<\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u6240\u6709\u6210\u5458\u8d77\u59cb\u5730\u5740\u76f8\u540c<\/li>\n<li>\u8054\u5408\u4f53\u5927\u5c0f = \u6700\u5927\u6210\u5458\u5927\u5c0f<\/li>\n<li>\u6bcf\u6b21\u53ea\u80fd\u6709\u6548\u4f7f\u7528\u5176\u4e2d\u201c\u4e00\u4e2a\u6210\u5458\u201d<\/li>\n\n<\/ul>\n<h2>15.1 \u5b9a\u4e49<\/h2>\n<pre><code class='language-cpp' lang='cpp'>union Data {\n    int   i;\n    float f;\n    char  str[20];\n};\n<\/code><\/pre>\n<h2>15.2 \u5171\u4eab\u5185\u5b58\u793a\u4f8b<\/h2>\n<pre><code>union Data d;\n\nd.i = 100;\nprintf(&quot;%d\\n&quot;, d.i);\n\n\/\/ \u518d\u5b58 float\uff0c\u4f1a\u8986\u76d6\u540c\u4e00\u5757\u5185\u5b58\nd.f = 3.14;\nprintf(&quot;%f\\n&quot;, d.f);\n\n\/\/ \u8bfb d.i \u662f\u672a\u5b9a\u4e49\u884c\u4e3a\n<\/code><\/pre>\n<h2>15.3 \u5e94\u7528\u573a\u666f<\/h2>\n<p>\u8054\u5408\u4f53\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u591a\u79cd\u53ef\u80fd\u7c7b\u578b\u7684\u6570\u636e\uff1a<\/p>\n<ol>\n<li>\u534f\u8bae\u89e3\u6790\uff08\u4f8b\u5982\u4e00\u4e2a\u5b57\u8282\u62c6\u4e3a bit \u6bb5\uff09<\/li>\n<li>\u6570\u636e\u7c7b\u578b\u91cd\u89e3\u91ca\uff08\u7c7b\u578b punning\uff09<\/li>\n<li>\u5d4c\u5165\u5f0f\u8bbe\u5907\u6620\u5c04\u5bc4\u5b58\u5668<\/li>\n<li>\u8282\u7701\u5185\u5b58\u7684\u7ed3\u6784\u4f53\u590d\u7528<\/li>\n\n<\/ol>\n<h2>15.4 \u8054\u5408\u4f53 + \u7ed3\u6784\u4f53\u6df7\u7528\uff08\u5e38\u89c1\uff09<\/h2>\n<pre><code class='language-cpp' lang='cpp'>typedef struct {\n    int type;\n    union {\n        int i;\n        float f;\n        char s[20];\n    } data;\n} Variant;\n<\/code><\/pre>\n<p>\u53ef\u5b9e\u73b0\u201c\u53ef\u53d8\u7c7b\u578b\u7ed3\u6784\u4f53\u201d\u3002<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>1. \u7ed3\u6784\u4f53\u7684\u6982\u5ff5 \u7ed3\u6784\u4f53\uff08struct\uff09\u7528\u4e8e\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u6210\u5458\u7ec4\u5408\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u4ee5\u63cf\u8ff0\u590d\u6742\u6570\u636e\u5b9e\u4f53\u3002 \u793a\u4f8b\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-362","post","type-post","status-publish","format-standard","hentry","category-23"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/posts\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":1,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":363,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions\/363"}],"wp:attachment":[{"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4.189.252.43\/index.php\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}