{"id":3422,"date":"2025-04-01T23:33:32","date_gmt":"2025-04-01T15:33:32","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3422"},"modified":"2025-04-01T23:33:32","modified_gmt":"2025-04-01T15:33:32","slug":"c-typedef-%e5%85%b3%e9%94%ae%e5%ad%97%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/01\/c-typedef-%e5%85%b3%e9%94%ae%e5%ad%97%e8%af%a6%e8%a7%a3\/","title":{"rendered":"C typedef \u5173\u952e\u5b57\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. <code>typedef<\/code> \u6982\u8ff0<\/strong><\/h2>\n\n\n\n<p><code>typedef<\/code> \u662f C \u8bed\u8a00\u4e2d\u7684\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u4e3a\u5df2\u6709\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u65b0\u7684\u540d\u79f0\uff08\u7c7b\u578b\u522b\u540d\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u5c24\u5176\u662f\u5728\u5b9a\u4e49\u590d\u6742\u7684\u7ed3\u6784\u4f53\u3001\u6307\u9488\u548c\u6570\u7ec4\u7c7b\u578b\u65f6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>typedef<\/code> \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef ExistingType NewType;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ExistingType<\/code>\uff1a\u539f\u59cb\u6570\u636e\u7c7b\u578b<\/li>\n\n\n\n<li><code>NewType<\/code>\uff1a\u65b0\u7684\u7c7b\u578b\u522b\u540d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>typedef<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u57fa\u672c\u7c7b\u578b\u522b\u540d<\/strong><\/h3>\n\n\n\n<p><code>typedef<\/code> \u53ef\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef unsigned int uint; \/\/ uint \u73b0\u5728\u662f unsigned int \u7684\u522b\u540d\n\nint main() {\n    uint x = 10;  \/\/ \u7b49\u4ef7\u4e8e unsigned int x = 10;\n    printf(\"x = %u\\n\", x);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u7ed3\u6784\u4f53\u4e0e <code>typedef<\/code><\/strong><\/h3>\n\n\n\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u5b9a\u4e49\u7ed3\u6784\u4f53\u65f6\u901a\u5e38\u9700\u8981 <code>struct<\/code> \u5173\u952e\u5b57\uff0c\u800c <code>typedef<\/code> \u53ef\u4ee5\u7b80\u5316\u7ed3\u6784\u4f53\u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>\u666e\u901a\u7ed3\u6784\u4f53\u58f0\u660e\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Student {\n    char name&#91;50];\n    int age;\n};\nstruct Student s1;<\/code><\/pre>\n\n\n\n<p><strong>\u4f7f\u7528 <code>typedef<\/code> \u7b80\u5316\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct {\n    char name&#91;50];\n    int age;\n} Student;  \/\/ \u76f4\u63a5\u4f7f\u7528 Student \u4f5c\u4e3a\u7c7b\u578b\u540d\n\nStudent s1; \/\/ \u73b0\u5728\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Student\uff0c\u800c\u65e0\u9700\u5199 struct<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u6307\u9488\u7c7b\u578b\u522b\u540d<\/strong><\/h3>\n\n\n\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u6307\u9488\u7684\u5b9a\u4e49\u901a\u5e38\u8f83\u590d\u6742\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>typedef<\/code> \u7b80\u5316\u3002<\/p>\n\n\n\n<p><strong>\u4e0d\u4f7f\u7528 <code>typedef<\/code> \u7684\u51fd\u6570\u6307\u9488\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int (*funcPtr)(int, int);<\/code><\/pre>\n\n\n\n<p><strong>\u4f7f\u7528 <code>typedef<\/code> \u7b80\u5316\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef int (*FuncType)(int, int);\nFuncType myFunc;  \/\/ myFunc \u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\ntypedef int (*FuncType)(int, int);\n\nint add(int a, int b) {\n    return a + b;\n}\n\nint main() {\n    FuncType f = add;\n    printf(\"Sum: %d\\n\", f(2, 3)); \/\/ \u8f93\u51fa Sum: 5\n    return 0;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 \u590d\u6742\u6570\u7ec4\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u5f53\u6570\u7ec4\u7c7b\u578b\u8f83\u590d\u6742\u65f6\uff0c<code>typedef<\/code> \u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<p><strong>\u666e\u901a\u5199\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char names&#91;10]&#91;20]; \/\/ 10 \u4e2a\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u6700\u591a 19 \u4e2a\u5b57\u7b26 + '\\0'<\/code><\/pre>\n\n\n\n<p><strong>\u4f7f\u7528 <code>typedef<\/code>\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef char NameList&#91;10]&#91;20]; \nNameList names;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. <code>typedef<\/code> \u4e0e <code>#define<\/code> \u7684\u533a\u522b<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>typedef<\/code><\/th><th><code>#define<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u4f5c\u7528\u8303\u56f4<\/td><td>\u7f16\u8bd1\u65f6\u5904\u7406<\/td><td>\u9884\u5904\u7406\u9636\u6bb5\u66ff\u6362<\/td><\/tr><tr><td>\u9002\u7528\u8303\u56f4<\/td><td>\u4ec5\u9002\u7528\u4e8e\u7c7b\u578b<\/td><td>\u9002\u7528\u4e8e\u4efb\u4f55\u4ee3\u7801\u7247\u6bb5<\/td><\/tr><tr><td>\u662f\u5426\u53ef\u8c03\u8bd5<\/td><td>\u662f\uff0c\u53ef\u7528\u4e8e\u8c03\u8bd5<\/td><td>\u4ec5\u6587\u672c\u66ff\u6362\uff0c\u8c03\u8bd5\u56f0\u96be<\/td><\/tr><tr><td>\u7c7b\u578b\u68c0\u67e5<\/td><td>\u7f16\u8bd1\u5668\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5<\/td><td>\u65e0\u7c7b\u578b\u68c0\u67e5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define uint unsigned int\ntypedef unsigned int uint;<\/code><\/pre>\n\n\n\n<p>\u4e24\u8005\u5728\u4f7f\u7528\u4e0a\u7c7b\u4f3c\uff0c\u4f46 <code>typedef<\/code> \u53d7\u7f16\u8bd1\u5668\u68c0\u67e5\uff0c\u800c <code>#define<\/code> \u53ea\u662f\u7b80\u5355\u7684\u6587\u672c\u66ff\u6362\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u7ed3\u5408 <code>struct<\/code> \u548c <code>enum<\/code> \u4f7f\u7528 <code>typedef<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 <code>typedef<\/code> + <code>struct<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct {\n    int id;\n    char name&#91;20];\n} Person;\n\nPerson p1;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 <code>typedef<\/code> + <code>enum<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef enum {\n    RED,\n    GREEN,\n    BLUE\n} Color;\n\nColor myColor = RED;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. <code>typedef<\/code> \u5e38\u89c1\u8bef\u533a<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bef\u533a 1\uff1a\u8ba4\u4e3a <code>typedef<\/code> \u5b9a\u4e49\u7684\u662f\u65b0\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p><code>typedef<\/code> \u53ea\u662f\u4e3a\u5df2\u6709\u7c7b\u578b\u521b\u5efa\u522b\u540d\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u771f\u6b63\u7684\u65b0\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef unsigned int uint;\nuint x = 5;  \/\/ x \u4ecd\u7136\u662f unsigned int<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bef\u533a 2\uff1a\u4f7f\u7528 <code>typedef<\/code> \u53ef\u4ee5\u7b80\u5316\u6307\u9488\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>\u9519\u8bef\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef char* String;\nString s1, s2;<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4ee5\u4e3a <code>s1<\/code> \u548c <code>s2<\/code> \u90fd\u662f <code>char*<\/code> \u7c7b\u578b\uff0c\u5b9e\u9645\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char* s1, s2;  \/\/ \u53ea\u6709 s1 \u662f\u6307\u9488\uff0cs2 \u53ea\u662f char<\/code><\/pre>\n\n\n\n<p>\u6b63\u786e\u7684\u505a\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef char* String;\nString s1;\nString s2;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. <code>typedef<\/code> \u5728\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u589e\u5f3a\u53ef\u8bfb\u6027<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   typedef unsigned char BYTE;\n   typedef unsigned long DWORD;<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5c01\u88c5\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   typedef struct {\n       int x, y;\n   } Point;<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u901a\u7528\u6570\u636e\u7c7b\u578b<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   typedef int BOOL;\n   #define TRUE 1\n   #define FALSE 0<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u7b80\u5316\u56de\u8c03\u51fd\u6570<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   typedef void (*Callback)(int);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>typedef<\/code> \u4e3a\u5df2\u6709\u7c7b\u578b\u521b\u5efa\u522b\u540d\uff0c\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li><code>typedef<\/code> \u9002\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u7ed3\u6784\u4f53\u3001\u51fd\u6570\u6307\u9488\u3001\u6570\u7ec4\u7b49\u3002<\/li>\n\n\n\n<li><code>typedef<\/code> \u4e0e <code>#define<\/code> \u7684\u533a\u522b\u5728\u4e8e\u7f16\u8bd1\u5668\u662f\u5426\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/li>\n\n\n\n<li><code>typedef<\/code> \u5728\u5927\u578b\u9879\u76ee\u4e2d\u5e7f\u6cdb\u7528\u4e8e\u5b9a\u4e49\u7c7b\u578b\uff0c\u63d0\u9ad8\u53ef\u79fb\u690d\u6027\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. typedef \u6982\u8ff0 typedef \u662f C \u8bed\u8a00\u4e2d\u7684\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u4e3a\u5df2\u6709\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u65b0\u7684\u540d\u79f0\uff08\u7c7b\u578b\u522b\u540d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3423,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3422","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\/3422","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=3422"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3422\/revisions"}],"predecessor-version":[{"id":3424,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3422\/revisions\/3424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3423"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}