{"id":73,"date":"2024-09-05T15:01:06","date_gmt":"2024-09-05T07:01:06","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=73"},"modified":"2024-09-05T15:01:06","modified_gmt":"2024-09-05T07:01:06","slug":"c%e8%af%ad%e8%a8%80%e5%b8%b8%e8%a7%81%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=73","title":{"rendered":"C\u8bed\u8a00\u5e38\u89c1\u6982\u5ff5"},"content":{"rendered":"<h3>1. <strong>\u5185\u5b58\u7ba1\u7406<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u52a8\u6001\u5206\u914d\u548c\u91ca\u653e\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main() {\n    int rows = 3, cols = 4;\n    \/\/ \u52a8\u6001\u5206\u914d\u4e8c\u7ef4\u6570\u7ec4\n    int **array = (int **)malloc(rows * sizeof(int *));\n    if (array == NULL) {\n        perror(&quot;Failed to allocate memory&quot;);\n        return 1;\n    }\n    for (int i = 0; i &lt; rows; i++) {\n        array[i] = (int *)malloc(cols * sizeof(int));\n        if (array[i] == NULL) {\n            perror(&quot;Failed to allocate memory&quot;);\n            \/\/ \u91ca\u653e\u5df2\u5206\u914d\u7684\u5185\u5b58\n            for (int j = 0; j &lt; i; j++) {\n                free(array[j]);\n            }\n            free(array);\n            return 1;\n        }\n    }\n\n    \/\/ \u521d\u59cb\u5316\u5e76\u6253\u5370\u6570\u7ec4\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            array[i][j] = i * cols + j;\n            printf(&quot;%d &quot;, array[i][j]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n\n    \/\/ \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i &lt; rows; i++) {\n        free(array[i]);\n    }\n    free(array);\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 <code>malloc<\/code> \u52a8\u6001\u5206\u914d\u5185\u5b58\u6765\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<li>\u68c0\u67e5\u6bcf\u6b21\u5206\u914d\u662f\u5426\u6210\u529f\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li>\u4f7f\u7528\u5b8c\u6bd5\u540e\uff0c\u4f7f\u7528 <code>free<\/code> \u91ca\u653e\u6240\u6709\u5206\u914d\u7684\u5185\u5b58\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>2. <strong>\u6307\u9488\u548c\u591a\u7ea7\u6307\u9488<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u7ea7\u6307\u9488\u4fee\u6539\u53d8\u91cf\u7684\u503c<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n    int value = 10;\n    int *ptr = &amp;value;\n    int **ptr2 = &amp;ptr;\n\n    printf(&quot;Original value: %d\\n&quot;, value); \/\/ \u8f93\u51fa 10\n    **ptr2 = 20;\n    printf(&quot;Modified value: %d\\n&quot;, value); \/\/ \u8f93\u51fa 20\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li><code>ptr<\/code> \u662f\u6307\u5411 <code>value<\/code> \u7684\u6307\u9488\u3002<\/li>\n<li><code>ptr2<\/code> \u662f\u6307\u5411 <code>ptr<\/code> \u7684\u6307\u9488\uff08\u591a\u7ea7\u6307\u9488\uff09\u3002<\/li>\n<li>\u901a\u8fc7 <code>**ptr2<\/code> \u4fee\u6539 <code>value<\/code> \u7684\u503c\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>3. <strong>\u6307\u9488\u8fd0\u7b97<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u6307\u9488\u4e0e\u6570\u7ec4\u7684\u5173\u7cfb<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n    int arr[] = {10, 20, 30, 40, 50};\n    int *ptr = arr; \/\/ \u6570\u7ec4\u540d\u4f5c\u4e3a\u6307\u9488\n\n    for (int i = 0; i &lt; 5; i++) {\n        printf(&quot;Element %d: %d\\n&quot;, i, *(ptr + i));\n    }\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u540d <code>arr<\/code> \u53ef\u4ee5\u4f5c\u4e3a\u6307\u9488\u4f7f\u7528\u3002<\/li>\n<li>\u901a\u8fc7\u6307\u9488\u8fd0\u7b97 <code>*(ptr + i)<\/code> \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>4. <strong>\u4f4d\u64cd\u4f5c<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u8bbe\u7f6e\u3001\u6e05\u9664\u548c\u5207\u6362\u4f4d<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n    unsigned int num = 0b0000; \/\/ \u521d\u59cb\u4e3a 0\n\n    \/\/ \u8bbe\u7f6e\u7b2c2\u4f4d\uff08\u4ece0\u5f00\u59cb\u8ba1\u6570\uff09\n    num |= (1 &lt;&lt; 2);\n    printf(&quot;After setting bit 2: %04b\\n&quot;, num); \/\/ \u8f93\u51fa 0100\n\n    \/\/ \u6e05\u9664\u7b2c2\u4f4d\n    num &amp;= ~(1 &lt;&lt; 2);\n    printf(&quot;After clearing bit 2: %04b\\n&quot;, num); \/\/ \u8f93\u51fa 0000\n\n    \/\/ \u5207\u6362\u7b2c1\u4f4d\n    num ^= (1 &lt;&lt; 1);\n    printf(&quot;After toggling bit 1: %04b\\n&quot;, num); \/\/ \u8f93\u51fa 0010\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26 <code>|<\/code>, <code>&amp;<\/code>, <code>^<\/code> \u6765\u8bbe\u7f6e\u3001\u6e05\u9664\u548c\u5207\u6362\u7279\u5b9a\u4f4d\u3002<\/li>\n<li><code>1 &lt;&lt; n<\/code> \u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u63a9\u7801\uff0c\u5c06\u7b2c <code>n<\/code> \u4f4d\u8bbe\u4e3a1\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>5. <strong>\u5b8f\u548c\u9884\u5904\u7406\u5668<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u5b8f\u5b9a\u4e49\u548c\u6761\u4ef6\u7f16\u8bd1<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\n#define PI 3.14159\n#define SQUARE(x) ((x) * (x))\n\n\/\/ \u6761\u4ef6\u7f16\u8bd1\n#define DEBUG\n\nint main() {\n    double radius = 5.0;\n    double area = PI * SQUARE(radius);\n    printf(&quot;Area: %.2f\\n&quot;, area);\n\n#ifdef DEBUG\n    printf(&quot;Debug Mode: radius=%.2f, area=%.2f\\n&quot;, radius, area);\n#endif\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li><code>#define<\/code> \u7528\u4e8e\u5b9a\u4e49\u5e38\u91cf\u548c\u5b8f\u51fd\u6570\u3002<\/li>\n<li>\u5b8f <code>SQUARE(x)<\/code> \u8ba1\u7b97 <code>x<\/code> \u7684\u5e73\u65b9\u3002<\/li>\n<li>\u4f7f\u7528 <code>#ifdef<\/code> \u8fdb\u884c\u6761\u4ef6\u7f16\u8bd1\uff0c\u6839\u636e\u662f\u5426\u5b9a\u4e49 <code>DEBUG<\/code> \u6765\u51b3\u5b9a\u662f\u5426\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>6. <strong>\u51fd\u6570\u6307\u9488<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u56de\u8c03<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\n\/\/ \u5b9a\u4e49\u51fd\u6570\u6307\u9488\u7c7b\u578b\ntypedef void (*Callback)(int);\n\n\/\/ \u4e00\u4e2a\u63a5\u53d7\u56de\u8c03\u51fd\u6570\u7684\u51fd\u6570\nvoid process(int data, Callback cb) {\n    \/\/ \u5904\u7406\u6570\u636e\n    data += 10;\n    \/\/ \u8c03\u7528\u56de\u8c03\u51fd\u6570\n    cb(data);\n}\n\n\/\/ \u56de\u8c03\u51fd\u6570\nvoid myCallback(int result) {\n    printf(&quot;Callback called with result: %d\\n&quot;, result);\n}\n\nint main() {\n    int value = 5;\n    process(value, myCallback); \/\/ \u4f20\u9012\u56de\u8c03\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7c7b\u578b <code>Callback<\/code>\uff0c\u6307\u5411\u63a5\u53d7 <code>int<\/code> \u53c2\u6570\u4e14\u8fd4\u56de <code>void<\/code> \u7684\u51fd\u6570\u3002<\/li>\n<li><code>process<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u548c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6307\u9488\uff0c\u5904\u7406\u6570\u636e\u540e\u8c03\u7528\u56de\u8c03\u3002<\/li>\n<li>\u5728 <code>main<\/code> \u4e2d\uff0c\u5c06 <code>myCallback<\/code> \u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7ed9 <code>process<\/code>\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>7. <strong>\u590d\u6742\u7684\u7ed3\u6784\u4f53\u548c\u8054\u5408\u4f53<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u5d4c\u5957\u7ed3\u6784\u4f53\u548c\u8054\u5408\u4f53<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\n\/\/ \u5b9a\u4e49\u65e5\u671f\u7ed3\u6784\u4f53\ntypedef struct {\n    int year;\n    int month;\n    int day;\n} Date;\n\n\/\/ \u5b9a\u4e49\u4eba\u5458\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u8054\u5408\u4f53\ntypedef struct {\n    char name[50];\n    int age;\n    Date birthdate;\n    union {\n        char studentID[20];\n        char employeeID[20];\n    } id;\n    int isStudent; \/\/ 1 \u8868\u793a\u5b66\u751f\uff0c0 \u8868\u793a\u5458\u5de5\n} Person;\n\nint main() {\n    Person p1;\n    strcpy(p1.name, &quot;Alice&quot;);\n    p1.age = 25;\n    p1.birthdate.year = 1998;\n    p1.birthdate.month = 5;\n    p1.birthdate.day = 15;\n    p1.isStudent = 1;\n    strcpy(p1.id.studentID, &quot;S12345678&quot;);\n\n    Person p2;\n    strcpy(p2.name, &quot;Bob&quot;);\n    p2.age = 30;\n    p2.birthdate.year = 1993;\n    p2.birthdate.month = 8;\n    p2.birthdate.day = 22;\n    p2.isStudent = 0;\n    strcpy(p2.id.employeeID, &quot;E87654321&quot;);\n\n    \/\/ \u6253\u5370\u4fe1\u606f\n    printf(&quot;Person 1: %s, Age: %d, Birthdate: %04d-%02d-%02d, Student ID: %s\\n&quot;,\n           p1.name, p1.age, p1.birthdate.year, p1.birthdate.month, p1.birthdate.day, p1.id.studentID);\n\n    printf(&quot;Person 2: %s, Age: %d, Birthdate: %04d-%02d-%02d, Employee ID: %s\\n&quot;,\n           p2.name, p2.age, p2.birthdate.year, p2.birthdate.month, p2.birthdate.day, p2.id.employeeID);\n\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li><code>Person<\/code> \u7ed3\u6784\u4f53\u5305\u542b\u4e00\u4e2a\u5d4c\u5957\u7684 <code>Date<\/code> \u7ed3\u6784\u4f53\u548c\u4e00\u4e2a\u8054\u5408\u4f53 <code>id<\/code>\uff0c\u6839\u636e <code>isStudent<\/code> \u6807\u5fd7\u4f7f\u7528\u4e0d\u540c\u7684 ID\u3002<\/li>\n<li>\u8054\u5408\u4f53 <code>id<\/code> \u53ef\u4ee5\u5b58\u50a8 <code>studentID<\/code> \u6216 <code>employeeID<\/code>\uff0c\u4f46\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>8. <strong>\u5f02\u5e38\u5904\u7406\u548c\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u8fd4\u56de\u503c\u548c\u9519\u8bef\u4ee3\u7801\u8fdb\u884c\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ \u5b9a\u4e49\u9519\u8bef\u4ee3\u7801\n#define SUCCESS 0\n#define ERROR_INVALID_INPUT 1\n#define ERROR_ALLOCATION_FAILED 2\n\n\/\/ \u51fd\u6570\uff1a\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c\nint calculate_average(int *arr, int size, double *average) {\n    if (arr == NULL || average == NULL) {\n        return ERROR_INVALID_INPUT;\n    }\n    if (size &lt;= 0) {\n        return ERROR_INVALID_INPUT;\n    }\n\n    long sum = 0;\n    for (int i = 0; i &lt; size; i++) {\n        sum += arr[i];\n    }\n    *average = (double)sum \/ size;\n    return SUCCESS;\n}\n\nint main() {\n    int data[] = {10, 20, 30, 40, 50};\n    double avg;\n    int status = calculate_average(data, 5, &amp;avg);\n\n    if (status == SUCCESS) {\n        printf(&quot;Average: %.2f\\n&quot;, avg);\n    } else if (status == ERROR_INVALID_INPUT) {\n        fprintf(stderr, &quot;Error: Invalid input provided.\\n&quot;);\n    } else if (status == ERROR_ALLOCATION_FAILED) {\n        fprintf(stderr, &quot;Error: Memory allocation failed.\\n&quot;);\n    } else {\n        fprintf(stderr, &quot;Error: Unknown error occurred.\\n&quot;);\n    }\n\n    return status;\n}<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li><code>calculate_average<\/code> \u51fd\u6570\u901a\u8fc7\u8fd4\u56de\u503c\u6307\u793a\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u5e76\u901a\u8fc7\u6307\u9488\u53c2\u6570\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<li>\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\uff0c\u6839\u636e\u8fd4\u56de\u7684\u72b6\u6001\u7801\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u548c\u4fe1\u606f\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>\u8fd9\u4e9b\u793a\u4f8b\u6db5\u76d6\u4e86 C \u8bed\u8a00\u4e2d\u4e00\u4e9b\u8f83\u4e3a\u590d\u6742\u7684\u6982\u5ff5\u548c\u6280\u672f\u3002\u901a\u8fc7\u7406\u89e3\u548c\u7ec3\u4e60\u8fd9\u4e9b\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u638c\u63e1 C \u8bed\u8a00\u7684\u9ad8\u7ea7\u7279\u6027\u548c\u5e95\u5c42\u64cd\u4f5c\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u5177\u4f53\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff0c\u8bf7\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5185\u5b58\u7ba1\u7406 \u793a\u4f8b\uff1a\u52a8\u6001\u5206\u914d\u548c\u91ca\u653e\u4e8c\u7ef4\u6570\u7ec4 #include &lt;stdio.h&gt; #includ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[14,29],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-29"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}