{"id":334,"date":"2023-09-16T10:32:10","date_gmt":"2023-09-16T02:32:10","guid":{"rendered":""},"modified":"2023-09-18T11:50:02","modified_gmt":"2023-09-18T03:50:02","slug":"c%e8%af%ad%e8%a8%80%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98-%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e7%9a%84%e5%9b%9b%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mushiming.com\/334.html","title":{"rendered":"C\u8bed\u8a00\u5e38\u89c1\u95ee\u9898\u2014\u2014\u6570\u7ec4\u521d\u59cb\u5316\u7684\u56db\u79cd\u65b9\u6cd5"},"content":{"rendered":"
\n\u6709\u4e00\u56de\uff0c\u5c0f\u6613\u540c\u5b66\u5bf9\u6211\u8bf4\uff1a\u201c\u4f60\u5b66\u8fc7\u7f16\u7a0b\u5417\u201d\uff0c\u6211\u7565\u7565\u70b9\u4e00\u70b9\u5934\u3002\u4ed6\u8bf4\uff0c\u201c\u5b66\u8fc7\u7f16\u7a0b\uff0c\u6211\u4fbf\u8003\u4e00\u8003\u4f60\uff0c\u5728\u4f7f\u7528\u6570\u7ec4\u4e4b\u524d\uff0c\u5e94\u4e0d\u5e94\u8be5\u521d\u59cb\u5316\u6570\u7ec4\uff1f<\/strong>\u201d<\/p>\n
\u6211\u6697\u60f3\uff0c\u8fd9\u4e48\u7b80\u5355\u7684\u95ee\u9898\uff0c\u4e5f\u914d\u8003\u6211\u4e48\uff1f\u4e0d\u53ea\u662f\u6570\u7ec4\uff0c\u5728\u4f7f\u7528\u4efb\u4f55\u53d8\u91cf\u4e4b\u524d\uff0c\u90fd\u5e94\u8be5\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u662f\u4e00\u4e2a\u7f16\u7a0b\u7684\u597d\u4e60\u60ef\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8bb8\u591a\u7531\u4e8e\u5783\u573e\u503c\u5bfc\u81f4\u7684bug\u3002<\/strong>\u4e8e\u662f\u6211\u56de\u8fc7\u8138\u53bb\uff0c\u4e0d\u518d\u7406\u4f1a\u3002<\/p>\n
\u5c0f\u6613\u540c\u5b66\u7b49\u4e86\u8bb8\u4e45\u4e5f\u4e0d\u89c1\u6211\u56de\u7b54\uff0c\u5f88\u8bda\u6073\u7684\u8bf4\u9053\uff0c\u201c\u4e0d\u77e5\u9053\u7f62\uff1f\u6211\u6559\u7ed9\u4f60\uff0c\u8bb0\u7740\uff01......\u6570\u7ec4\u7684\u521d\u59cb\u5316\u67094\u79cd\u5199\u6cd5<\/strong>\u2014\u2014\u201d<\/p>\n<\/blockquote>\n
\u76ee\u5f55<\/strong><\/p>\n
1.\u521d\u59cb\u5316\u53c2\u6570\u5217\u8868<\/p>\n
2.\u4f7f\u7528memset\u51fd\u6570\u521d\u59cb\u5316(\u53ea\u5efa\u8bae\u7528\u4e8e\u5b57\u7b26\u6570\u7ec4)<\/p>\n
3.\u6307\u5b9a\u521d\u59cb\u5316\u5668(gcc\u652f\u6301)<\/p>\n
4.\u4f7f\u7528for\u5faa\u73af\u521d\u59cb\u5316<\/p>\n
5.\u603b\u7ed3<\/p>\n
\n\n
1.\u521d\u59cb\u5316\u53c2\u6570\u5217\u8868<\/strong><\/strong><\/h2>\n
\u5168\u90e8\u521d\u59cb\u5316\u4e3a0\uff1a<\/strong><\/p>\n
int a[10]={0};\nchar str[10]=\"\\0\"; \/\/\u7b49\u4ef7\u4e8echar str[10]={0};\n<\/code><\/pre>\n
\u521d\u59cb\u5316\u4e3a\u5176\u4ed6\u503c\uff1a<\/strong><\/p>\n
int a[10]={0,1,2,3,4,5,6,7,8,9};\nchar str[10]=\"Hello\"; \/\/\u4e5f\u53ef\u4ee5\u5199\u6210char str[10]={'H','e','l','l','o','\\0'}<\/code><\/pre>\n
\u5982\u679c\u521d\u59cb\u5316\u5217\u8868\u5305\u542b\u6570\u7ec4a\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u7701\u7565\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n
int a[]={0,1,2,3,4,5,6,7,8,9};<\/code><\/pre>\n
\u591a\u7ef4\u6570\u7ec4\u5728\u8ba1\u7b97\u673a\u4e2d\u4e5f\u662f\u7ebf\u6027\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u4e0b\u9762\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7\uff1a<\/p>\n
int a[2][5]={\n \n {0,1,2,3,4},{5,6,7,8,9}};\n\/\/int a[2][5]={0,1,2,3,4,5,6,7,8,9};<\/code><\/pre>\n
\n\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u60f3\u628a\u6570\u7ec4\u5143\u7d20\u5168\u90e8\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u975e\u96f6\u503c\uff08\u6bd4\u59821\uff09\uff0c<\/span>\u4e0d\u80fd\u4f7f\u7528<\/strong><\/span><\/strong>int a[10]={
\n <\/strong><\/span><\/strong>1<\/strong><\/span><\/strong>}<\/strong><\/span><\/strong>\u8fd9\u79cd\u65b9\u5f0f<\/strong><\/span><\/strong>\u3002<\/span>int a[10]={1}\u53ea\u4f1a\u628aa[0]\u521d\u59cb\u5316\u4e3a1\uff0c\u800c\u628a\u5176\u4ed6\u5143\u7d20\u521d\u59cb\u5316\u4e3a0\uff08C99\u6807\u51c6\u4e0b\uff09\u3002<\/p>\n<\/p>\n<\/blockquote>\n
2.\u4f7f\u7528memset\u51fd\u6570\u521d\u59cb\u5316(\u53ea\u5efa\u8bae\u7528\u4e8e\u5b57\u7b26\u6570\u7ec4)<\/strong><\/strong><\/h2>\n
memset\u51fd\u6570\u7684\u5b9a\u4e49\u5305\u542b\u5728\u5934\u6587\u4ef6string.h<\/strong>\u4e2d\uff0c\u5176\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n
void *memset(void *s, int c, unsigned long n);<\/strong><\/span><\/strong><\/p>\n
\/\/memset\u51fd\u6570\uff1a\u5c06s\u6307\u5411\u7684\u5185\u5b58\u4f4d\u7f6e\u540e\u9762n\u4e2a\u5b57\u8282\u7684\u5b58\u50a8\u7a7a\u95f4\u66ff\u6362\u4e3ac<\/span><\/p>\n
#include<stdio.h>\n#include<string.h>\nint main(){\n\tint i=0;\n\tchar a[10];\n\tmemset(a,'a',sizeof(a));\/\/\u521d\u59cb\u5316\n\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%c \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}<\/code><\/pre>\n
<\/p>\n
\n\u6ce8\u610fmemset\u901a\u5e38\u53ea\u7528\u4e8e\u7528\u4e8e\u5b57\u7b26\u6570\u7ec4\u7684\u521d\u59cb\u5316<\/span><\/strong>\uff0c\u56e0\u4e3amemset\u66ff\u6362\u5185\u5b58\u7a7a\u95f4\u7684\u503c\u65f6\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\uff08char\u7c7b\u578b\u6070\u597d\u53ea\u53601\u5b57\u8282\uff0c\u800cint\u7c7b\u578b\u4e00\u822c\u53604\u5b57\u8282\uff09<\/p>\n
\u5982\u679c\u4f60\u7528memset\u6765\u521d\u59cb\u5316int\u6570\u7ec4\u5c31\u4f1a\u8fd9\u6837\uff1a<\/p>\n
<\/p>\n
\u56e0\u4e3a(00000001 <\/span>00000001 00000001 00000001<\/span>)<\/span>B<\/span>=(16843009)D<\/span><\/p>\n<\/blockquote>\n
\n\u5982\u679c\u4f7f\u7528memset\u521d\u59cb\u5316int\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u53ea\u80fd\u8bbe\u4e3a0\u6216-1<\/strong><\/span><\/p>\n
#include<stdio.h>\n#include<string.h>\nint main(){\n\tint i=0;\n\tint a[10];\n\n\tmemset(a,-1,sizeof(a));\/\/\u5168\u90e8\u66ff\u6362\u4e3a-1\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"\\n\");\n\n\tmemset(a,0,sizeof(a));\/\/\u5168\u90e8\u66ff\u6362\u4e3a0\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}<\/code><\/pre>\n
<\/p>\n
\u56e0\u4e3a(00000000 00000000 00000000 00000000)B=(0)D\uff0c<\/span><\/p>\n
(11111111 11111111 11111111 11111111)B=(-1)D<\/span><\/p>\n<\/blockquote>\n
3.\u6307\u5b9a\u521d\u59cb\u5316\u5668(gcc\u652f\u6301)<\/strong><\/strong><\/h2>\n
\u6307\u5b9a\u521d\u59cb\u5316\u5668<\/strong>\u662fC99\u6807\u51c6\u65b0\u589e\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5f62\u5982int a[10]={[0 ... 9]=1}<\/span><\/span>\u6216int a[10]={[1]=1,[2]=2,[9]=9}<\/span><\/span>\uff0c\u53ef\u4ee5\u521d\u59cb\u5316\u6307\u5b9a\u7684\u6570\u7ec4\u5143\u7d20\u3002<\/span><\/p>\n
\u7ecf\u7b14\u8005\u6d4b\u8bd5\uff0c\u53ea\u6709<\/span>gcc\u7f16\u8bd1\u5668<\/strong><\/span>(Codeblocks\u7684.c\u6587\u4ef6\u5c31\u662f\u7528gcc\u7f16\u8bd1\u7684)\u652f\u6301\u8be5\u7279\u6027\u3002vc++6.0\u4ee5\u53cag++\u7f16\u8bd1\u5668\u5747\u4e0d\u652f\u6301\u6307\u5b9a\u521d\u59cb\u5316\u5668\u3002<\/span><\/p>\n
\n#include<stdio.h>\nint main(){\n\tint i=0;\n\tint a[10]={[0 ... 9]=1};\n\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}<\/code><\/pre>\n
<\/p>\n
\n#include<stdio.h>\nint main(){\n\tint i=0;\n\tint a[10]={[1]=1,[2]=2,[9]=9};\n\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}<\/code><\/pre>\n
<\/p>\n<\/blockquote>\n
4.\u4f7f\u7528for\u5faa\u73af\u521d\u59cb\u5316<\/strong><\/strong><\/h2>\n
#include<stdio.h>\nint main(){\n\tint i=0;\n\tint a[10];\n\tfor(i=0;i<10;i++)\n\t\ta[i]=i;\n\t\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}<\/code><\/pre>\n
<\/p>\n
5.\u603b\u7ed3<\/h2>\n
\u6bd4\u8f83\u4e00\u4e0b\u6570\u7ec4\u521d\u59cb\u5316\u7684\u56db\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/p>\n
\n
- \u521d\u59cb\u5316\u53c2\u6570\u5217\u8868<\/strong>\uff1a\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u521d\u59cb\u5316\u4e3a0\u65f6\u975e\u5e38\u65b9\u4fbf\u3002\u521d\u59cb\u5316\u901f\u5ea6\u8f83\u5feb\uff1b\u4f46\u662f\u5f53\u9700\u8981\u628a\u4e00\u4e2a\u5f88\u957f\u7684\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u975e\u96f6\u503c\u65f6\u6bd4\u8f83\u9ebb\u70e6\u3002<\/li>\n
- \u4f7f\u7528memset\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316<\/strong>\uff1a\u9002\u5408\u4e8echar\u7c7b\u578b\u6570\u7ec4\u7684\u521d\u59cb\u5316\uff0c\u901f\u5ea6\u8f83\u5feb\uff1b\u7f3a\u70b9\u662fmemset\u4e0d\u9002\u5408\u521d\u59cb\u5316char\u7c7b\u578b\u4ee5\u5916\u7684\u6570\u7ec4\u3002<\/li>\n
- \u6307\u5b9a\u521d\u59cb\u5316\u5668<\/strong>\uff1a\u4f7f\u7528\u65b9\u4fbf\uff0c\u521d\u59cb\u5316\u901f\u5ea6\u8f83\u5feb\uff1b\u7f3a\u70b9\u662f\u5f88\u591a\u7f16\u8bd1\u5668\u4e0d\u652f\u6301\u8be5\u7279\u6027\uff0c\u5e76\u4e14C++\u7684\u8bed\u6cd5\u4e5f\u4e0d\u5141\u8bb8\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u5668\uff0c\u53ef\u79fb\u690d\u6027\u8f83\u5dee\u3002<\/li>\n
- for\u5faa\u73af\u521d\u59cb\u5316<\/strong>\uff1a\u7f3a\u70b9\u662f\u901f\u5ea6\u8f83\u6162\uff1b\u4f46\u662ffor\u5faa\u73af\u7684\u79fb\u690d\u6027\u8f83\u597d\uff0c\u51e0\u4e4e\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u6570\u7ec4\u7684\u521d\u59cb\u5316\uff08\u53ef\u4ee5\u8003\u8651\u548c\u521d\u59cb\u5316\u53c2\u6570\u5217\u8868int a[10]={0}\u914d\u5408\u4f7f\u7528\uff0c\u5e76\u4e14release\u7248\u672c\u7684for\u5faa\u73af\u521d\u59cb\u5316\u7ecf\u4f18\u5316\u540e\u901f\u5ea6\u5e76\u4e0d\u6162\uff09<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u5e38\u89c1\u95ee\u9898\u2014\u2014\u6570\u7ec4\u521d\u59cb\u5316\u7684\u56db\u79cd\u65b9\u6cd5\u6709\u4e00\u56de\uff0c\u5c0f\u6613\u540c\u5b66\u5bf9\u6211\u8bf4\uff1a\u201c\u4f60\u5b66\u8fc7\u7f16\u7a0b\u5417\u201d\uff0c\u6211\u7565\u7565\u70b9\u4e00\u70b9\u5934\u3002\u4ed6\u8bf4\uff0c\u201c\u5b66\u8fc7\u7f16\u7a0b\uff0c\u6211\u4fbf\u8003\u4e00...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/334"}],"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=334"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}