{"id":324,"date":"2023-02-22T02:36:19","date_gmt":"2023-02-22T10:36:19","guid":{"rendered":"http:\/\/bitas.net.cn\/?page_id=324"},"modified":"2023-02-22T02:36:25","modified_gmt":"2023-02-22T10:36:25","slug":"117-2","status":"publish","type":"page","link":"http:\/\/bitas.net.cn\/index.php\/sourcecode\/117-2\/","title":{"rendered":"117 \u52a8\u6001\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;string.h>\n#include &lt;assert.h>\n\ntypedef struct tagHyperIDCard {\n    long serialNo;\n    char name&#91;32];\n    char *phoneNo&#91;];\n} HyperIDCard;\n\nint main(int argc, char *argv&#91;])\n{\n    char *phoneNoA, *phoneNoB;\n    \/\/ allocate memory for HyperIDCard\n    HyperIDCard *id = calloc(1, sizeof(HyperIDCard));\n    if (id == NULL)\n        return -1;\n    \/\/ calloc should clear allocated memory\n    assert(id->serialNo == 0);\n    for (int i = 0; i &lt; sizeof(id->name); i++)\n        assert(id->name&#91;i] == 0);\n    \/\/ re-allocate memory for phone No.\n    id = realloc(id,\n        1 * sizeof(HyperIDCard) + 4);\n    if (id == NULL)\n        return -1; \n    \/\/ allocate memory for phoneNoA\n    if ((phoneNoA = malloc(12)) != NULL) {\n        strcpy(phoneNoA, \"13100000001\");\n        id->phoneNo&#91;0] = phoneNoA;\n    }   \n    else {\n        free(id);\n        return -1; \n    }\n    \/\/ add another phone No\n    id = realloc(id,\n        1 * sizeof(HyperIDCard) + 8); \n    if (id == NULL) {\n        free(phoneNoA);\n        return -1; \n    }   \n    id->serialNo = 200301;\n    strcpy(id->name, \"x301\");\n    id->phoneNo&#91;0] = phoneNoA;\n    if ((phoneNoB = malloc(12)) != NULL) {\n        strcpy(phoneNoB, \"13100000002\");\n        id->phoneNo&#91;1] = phoneNoB;\n    }\n    else {\n        free(phoneNoA);\n        free(id);\n    }\n    \/\/ show all phone No\n    printf(\"%06ld\\n\"\n        \"%6s\\n\"\n        \"%s\\n\"\n        \"%s\\n\"\n        \"%s\\n\",\n        id->serialNo,\n        \"------\",\n        id->name,\n        id->phoneNo&#91;0],\n        id->phoneNo&#91;1]);\n    \/\/ release all allocated memory\n    free(phoneNoB);\n    free(phoneNoA);\n    free(id);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":127,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-324","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":1,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/324\/revisions"}],"predecessor-version":[{"id":325,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/324\/revisions\/325"}],"up":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/127"}],"wp:attachment":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/media?parent=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}