{"id":6794,"date":"2024-03-23T07:01:01","date_gmt":"2024-03-22T23:01:01","guid":{"rendered":""},"modified":"2024-03-23T07:01:01","modified_gmt":"2024-03-22T23:01:01","slug":"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6794.html","title":{"rendered":"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03"},"content":{"rendered":"

\u770b\u5230\u4e00\u4e2a\u4e4b\u524d\u4e00\u76f4\u6ca1\u6709\u610f\u8bc6\u5230\u7684\u95ee\u9898\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

#include <stdio.h>\n#include <string.h>\n\ntypedef struct helloTest {\n\tchar a[4];\n\tchar b[24];\n}helloTest_t;\n\nint main (void)\n{\n\n\thelloTest_t t1;\n\tprintf(\"address,a:%p, b:%p\\n\", &t1.a, &t1.b);\n\n\tstrcpy(t1.b, \"hello world!\");\n\tstrcpy(t1.a, \"hello\");\n\tprintf(\"%s\\n\", t1.b);\n\treturn 0;\n}\n<\/code><\/pre>\n

\u601d\u8003printf\u8f93\u51fa\u7684\u662f\u4ec0\u4e48\uff0c\u5982\u679c\u4f60\u7684\u7b54\u6848\u662f\u201chello world\uff01\u201d\uff0c\u90a3\u8fd9\u7bc7\u6587\u7ae0\u8bf7\u63a5\u7740\u5f80\u4e0b\u770b\u3002<\/p>\n

\u7b54\u6848\u662f\uff1ao<\/p>\n

\n

address,a:0x7fff1219ddc0, b:0x7fff1219ddc4
o<\/p>\n<\/blockquote>\n

 \u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n

\u770b\u7ed3\u6784\u4f53\u4e2d\u7684a\uff0cb\u53d8\u91cf\uff0c\u5176\u5185\u5b58\u5206\u5e03\u662f\u8fde\u7eed\u7684\uff0c\u90a3\u4e5f\u5c31\u662f\uff0c\u5f53\u6211\u4eceb\u7684\u9996\u5730\u5740\u5148\u62f7\u8d1d\u201chello world\uff01\u201d\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662f\u4ecec4\u5730\u5740\u5f00\u59cb\uff0c\u6a21\u578b\u5982\u4e0b\uff1a<\/p>\n

\"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03<\/p>\n

\u7136\u540e\u4ecea\u7684\u9996\u5730\u5740c0\u5f00\u59cb\u62f7\u8d1d\u201chello\u201d\uff0c\u5185\u5b58\u5206\u5e03\u5219\u53d8\u6210\uff1a<\/p>\n

\"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03 <\/p>\n

\u6240\u4ee5\u6253\u5370t1.b\u65f6\uff0c\u5373\u4ecec4\u5730\u5740\u5f00\u59cb\uff0c\u9047\u5230'\\0'\u505c\u6b62\uff0c\u8f93\u51fa\uff1ao\u3002<\/p>\n

    \n
  • \u5982\u679c\u7a0b\u5e8f\u53d8\u4e3a\u8fd9\u6837\u5462\uff1a<\/li>\n<\/ul>\n
    #include <stdio.h>\n#include <string.h>\n\ntypedef struct helloTest {\n\tchar a[4];\n\tchar b[24];\n}helloTest_t;\n\nint main (void)\n{\n\n\thelloTest_t t1;\n\tprintf(\"address,a:%p, b:%p\\n\", &t1.a, &t1.b);\n\n\tstrcpy(t1.a, \"hello\");\n\tprintf(\"%c\\n\", t1.a[4]);\n\treturn 0;\n}\n<\/code><\/pre>\n

    \u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u7a0b\u5e8f\u5bf9\u6570\u7ec4a\u7684\u8bbf\u95ee\uff0c\u6709\u8d8a\u754c\u884c\u4e3a\uff0c\u731c\u4e00\u4e0b\u80fd\u7f16\u8bd1\u5e76\u8fd0\u884c\u6210\u529f\u5417\uff1f<\/p>\n

    \u7b54\u6848\u662f\u53ef\u4ee5\uff0c\u8f93\u51fa\uff1ao<\/p>\n

            \u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5206\u914d\u662f\u8fde\u7eed\u7684\uff0c\u6240\u4ee5\u4ece\u6570\u7ec4a\u7684\u9996\u5730\u5740\u5f00\u59cb\u62f7\u8d1d\uff0c\u201chello\u201d \u52a0\u4e0a'\\0'\u603b\u51716\u4e2a\u5b57\u8282\uff0c\u662f\u6ca1\u6709\u8d85\u8fc7\u7ed3\u6784\u4f53\u603b\u7684\u957f\u5ea6\u7684\uff0828Byte\uff09\uff0c\u6240\u4ee5\u8be5\u7a0b\u5e8f\u4e0d\u4f1a\u62a5\u9519\uff0c\u4e14\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n

            \u6709\u4eba\u95ee\uff0ca[4]\u4e0d\u662f\u8d8a\u754c\u4e86\u561b<\/p>\n

            a[4] \u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u4ee5a\u7684\u9996\u5730\u5740\uff0c\u4e5f\u5c31\u662fc0\u5f00\u59cb\u504f\u79fb4Byte\uff0c\u4e5f\u5c31\u662f*(t1.a+4);<\/p>\n

      \n
    • \u518d\u601d\u8003\u4e00\u4e2a\uff0c\u5982\u679c\u628a\u7ed3\u6784\u4f53\u4e2d\u7684b\u53d8\u91cf\u53bb\u6389\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/li>\n<\/ul>\n

      \u8fd9\u91cc\u6709\u70b9\u79bb\u8c31\uff0c\u5728linux\u4e2d\u7f16\u8bd1\u7684\uff0c\u7adf\u7136\u6ca1\u6709\u62a5\u9519\uff0c\u4e14\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u53ef\u80fd\u662fgcc\u7248\u672c\u6709\u70b9\u4f4e\u3002\u4ece\u800c\u80fd\u770b\u51fastrcpy\u8fd9\u4e2a\u51fd\u6570\u6709\u591a\u5371\u9669\uff0c\u614e\u7528\uff01\uff01\uff01<\/p>\n

      \"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03<\/p>\n

       \u8bf7\u7528strncpy_s(char * str2, int size2, char * str1, int size1);\u4ee3\u66ff<\/p>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00-struct\u5185\u5b58\u5206\u5e03\u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5206\u914d\u662f\u8fde\u7eed\u7684\uff0c\u6240\u4ee5\u4ece\u6570\u7ec4a\u7684\u9996\u5730\u5740\u5f00\u59cb\u62f7\u8d1d\uff0c\u201chello\u201d\u52a0\u4e0a'\\0'\u603b\u51716\u4e2a\u5b57\u8282\uff0c\u662f\u6ca1\u6709\u8d85\u8fc7...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6794"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6794"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6794\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}