{"id":3416,"date":"2025-04-01T23:30:48","date_gmt":"2025-04-01T15:30:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3416"},"modified":"2025-04-01T23:30:48","modified_gmt":"2025-04-01T15:30:48","slug":"c-%e8%af%ad%e8%a8%80%e7%bb%93%e6%9e%84%e4%bd%93%ef%bc%88struct%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/01\/c-%e8%af%ad%e8%a8%80%e7%bb%93%e6%9e%84%e4%bd%93%ef%bc%88struct%ef%bc%89\/","title":{"rendered":"C \u8bed\u8a00\u7ed3\u6784\u4f53\uff08Struct\uff09"},"content":{"rendered":"\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c<strong>\u7ed3\u6784\u4f53\uff08Struct\uff09<\/strong> \u662f\u4e00\u79cd\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u7ed3\u6784\u4f53\u4f7f\u5f97\u4f60\u80fd\u591f\u5728\u4e00\u4e2a\u5355\u4e00\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\u4e2d\uff0c\u5305\u542b\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u6210\u5458\uff0c\u8fd9\u4e9b\u6210\u5458\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\uff0c\u751a\u81f3\u662f\u5176\u4ed6\u7ed3\u6784\u4f53\u6216\u6570\u7ec4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. C \u7ed3\u6784\u4f53\u7684\u57fa\u672c\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u4f7f\u7528 <code>struct<\/code> \u5173\u952e\u5b57\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct StructName {\n    dataType member1;\n    dataType member2;\n    ...\n};<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c<code>StructName<\/code> \u662f\u7ed3\u6784\u4f53\u7684\u540d\u79f0\uff0c<code>member1<\/code>, <code>member2<\/code> \u7b49\u662f\u7ed3\u6784\u4f53\u7684\u6210\u5458\uff0c<code>dataType<\/code> \u662f\u6210\u5458\u7684\u6570\u636e\u7c7b\u578b\u3002\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u6570\u7ec4\u3001\u6307\u9488\u751a\u81f3\u5176\u4ed6\u7ed3\u6784\u4f53\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 \u5b9a\u4e49\u548c\u58f0\u660e\u7ed3\u6784\u4f53<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n\/\/ \u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u578b\nstruct Person {\n    char name&#91;50];\n    int age;\n    float height;\n};\n\nint main() {\n    \/\/ \u58f0\u660e\u5e76\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u53d8\u91cf\n    struct Person p1 = {\"Alice\", 30, 5.6};\n\n    \/\/ \u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\n    printf(\"Name: %s\\n\", p1.name);\n    printf(\"Age: %d\\n\", p1.age);\n    printf(\"Height: %.2f\\n\", p1.height);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Alice\nAge: 30\nHeight: 5.60<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u7ed3\u6784\u4f53\u6210\u5458\u7684\u8bbf\u95ee<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u6210\u5458\u7684\u8bbf\u95ee\u53ef\u4ee5\u901a\u8fc7\u70b9\uff08<code>.<\/code>\uff09\u8fd0\u7b97\u7b26\u6765\u5b8c\u6210\u3002\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fd0\u7b97\u7b26\u8bbf\u95ee\u7ed3\u6784\u4f53\u53d8\u91cf\u4e2d\u7684\u7279\u5b9a\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p1.name       \/\/ \u8bbf\u95ee name \u6210\u5458\np1.age        \/\/ \u8bbf\u95ee age \u6210\u5458\np1.height     \/\/ \u8bbf\u95ee height \u6210\u5458<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u7ed3\u6784\u4f53\u6307\u9488<\/strong><\/h3>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u3002\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u6307\u9488\u65f6\uff0c\u8bbf\u95ee\u6210\u5458\u9700\u8981\u4f7f\u7528\u7bad\u5934\uff08<code>-&gt;<\/code>\uff09\u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.1 \u7ed3\u6784\u4f53\u6307\u9488\u7684\u58f0\u660e\u548c\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct Person {\n    char name&#91;50];\n    int age;\n    float height;\n};\n\nint main() {\n    struct Person p1 = {\"Bob\", 25, 5.9};\n    struct Person *ptr = &amp;p1;  \/\/ \u7ed3\u6784\u4f53\u6307\u9488\u6307\u5411 p1\n\n    \/\/ \u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u8bbf\u95ee\u6210\u5458\n    printf(\"Name: %s\\n\", ptr-&gt;name);\n    printf(\"Age: %d\\n\", ptr-&gt;age);\n    printf(\"Height: %.2f\\n\", ptr-&gt;height);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Bob\nAge: 25\nHeight: 5.90<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2 \u4f7f\u7528\u6307\u9488\u4fee\u6539\u7ed3\u6784\u4f53\u6210\u5458<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr-&gt;age = 26;  \/\/ \u901a\u8fc7\u6307\u9488\u4fee\u6539\u7ed3\u6784\u4f53\u6210\u5458<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u7ed3\u6784\u4f53\u7684\u521d\u59cb\u5316<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521d\u59cb\u5316\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.1 \u9759\u6001\u521d\u59cb\u5316<\/strong><\/h4>\n\n\n\n<p>\u5728\u58f0\u660e\u65f6\u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person p1 = {\"Alice\", 30, 5.6};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.2 \u52a8\u6001\u521d\u59cb\u5316<\/strong><\/h4>\n\n\n\n<p>\u901a\u8fc7\u4e00\u4e2a\u7ed3\u6784\u4f53\u6307\u9488\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person *p1 = (struct Person *)malloc(sizeof(struct Person));\nstrcpy(p1-&gt;name, \"Alice\");\np1-&gt;age = 30;\np1-&gt;height = 5.6;<\/code><\/pre>\n\n\n\n<p>\u8bb0\u5f97\u5728\u4f7f\u7528 <code>malloc<\/code> \u5206\u914d\u5185\u5b58\u540e\uff0c\u9700\u8981\u901a\u8fc7 <code>free<\/code> \u91ca\u653e\u5185\u5b58\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>free(p1);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u7ed3\u6784\u4f53\u7684\u5d4c\u5957<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u53ef\u4ee5\u5d4c\u5957\uff0c\u5373\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct Address {\n    char city&#91;50];\n    char street&#91;100];\n};\n\nstruct Person {\n    char name&#91;50];\n    int age;\n    struct Address address;  \/\/ \u5d4c\u5957\u7ed3\u6784\u4f53\n};\n\nint main() {\n    struct Person p1 = {\"Alice\", 30, {\"New York\", \"123 Broadway\"}};\n\n    printf(\"Name: %s\\n\", p1.name);\n    printf(\"Age: %d\\n\", p1.age);\n    printf(\"City: %s\\n\", p1.address.city);\n    printf(\"Street: %s\\n\", p1.address.street);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Alice\nAge: 30\nCity: New York\nStreet: 123 Broadway<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u7ed3\u6784\u4f53\u7684\u5927\u5c0f<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u901a\u5e38\u7b49\u4e8e\u5176\u6240\u6709\u6210\u5458\u7684\u5927\u5c0f\u4e4b\u548c\uff0c\u4f46\u7531\u4e8e\u5185\u5b58\u5bf9\u9f50\uff08padding\uff09\uff0c\u5b9e\u9645\u5927\u5c0f\u53ef\u80fd\u7565\u5927\u4e8e\u6210\u5458\u5927\u5c0f\u7684\u603b\u548c\u3002\u5185\u5b58\u5bf9\u9f50\u7684\u76ee\u7684\u662f\u4f18\u5316 CPU \u8bbf\u95ee\u5185\u5b58\u7684\u901f\u5ea6\u3002\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5bf9\u9f50\u901a\u5e38\u662f\u6309\u6700\u5927\u6210\u5458\u7684\u5927\u5c0f\u5bf9\u9f50\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6.1 \u67e5\u770b\u7ed3\u6784\u4f53\u7684\u5927\u5c0f<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct Person {\n    char name&#91;50];\n    int age;\n    float height;\n};\n\nint main() {\n    printf(\"Size of struct Person: %lu\\n\", sizeof(struct Person));\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of struct Person: 56<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684\u7ed3\u679c\u53ef\u80fd\u5305\u542b\u586b\u5145\u5b57\u8282\uff08padding\uff09\uff0c\u4ee5\u786e\u4fdd\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5bf9\u9f50\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u7ed3\u6784\u4f53\u4e0e\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u662f\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u6570\u7ec4\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u7d22\u5f15\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person {\n    char name&#91;50];\n    int age;\n    float height;\n};\n\nint main() {\n    struct Person people&#91;3] = {\n        {\"Alice\", 30, 5.6},\n        {\"Bob\", 25, 5.9},\n        {\"Charlie\", 35, 6.1}\n    };\n\n    for (int i = 0; i &lt; 3; i++) {\n        printf(\"Name: %s, Age: %d, Height: %.2f\\n\", people&#91;i].name, people&#91;i].age, people&#91;i].height);\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Alice, Age: 30, Height: 5.60\nName: Bob, Age: 25, Height: 5.90\nName: Charlie, Age: 35, Height: 6.10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. \u7ed3\u6784\u4f53\u7684\u7c7b\u578b\u522b\u540d<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>typedef<\/code> \u4e3a\u7ed3\u6784\u4f53\u7c7b\u578b\u521b\u5efa\u522b\u540d\uff0c\u4ee5\u4fbf\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\ntypedef struct {\n    char name&#91;50];\n    int age;\n    float height;\n} Person;\n\nint main() {\n    Person p1 = {\"Alice\", 30, 5.6};\n    printf(\"Name: %s, Age: %d, Height: %.2f\\n\", p1.name, p1.age, p1.height);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. \u7ed3\u6784\u4f53\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8868\u793a\u590d\u5408\u6570\u636e<\/strong>\uff1a\u7ed3\u6784\u4f53\u901a\u5e38\u7528\u4e8e\u8868\u793a\u5305\u542b\u591a\u79cd\u7c7b\u578b\u7684\u590d\u5408\u6570\u636e\uff0c\u5982\u5b66\u751f\u4fe1\u606f\u3001\u5458\u5de5\u6570\u636e\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u548c\u52a8\u6001\u5206\u914d\u6765\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5c01\u88c5<\/strong>\uff1a\u7ed3\u6784\u4f53\u6709\u52a9\u4e8e\u6570\u636e\u5c01\u88c5\uff0c\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u51fd\u6570\u7ed3\u5408\u8d77\u6765\uff08\u5982\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5c01\u88c5\u601d\u60f3\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.runoob.com\/cprogramming\/c-struct.html\">C\u7ed3\u6784\u4f53\u6559\u7a0b &#8211; \u83dc\u9e1f\u6559\u7a0b<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2310.pdf\">ISO C Standard &#8211; Struct<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u7ed3\u6784\u4f53\uff08Struct\uff09 \u662f\u4e00\u79cd\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u7ed3\u6784\u4f53\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":3418,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3416\/revisions\/3418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3417"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}