{"id":214736,"date":"2021-04-07T08:52:34","date_gmt":"2021-04-07T00:52:34","guid":{"rendered":"https:\/\/gulass.cn\/?p=214736"},"modified":"2021-03-24T16:55:08","modified_gmt":"2021-03-24T08:55:08","slug":"how-to-realize-2","status":"publish","type":"post","link":"https:\/\/gulass.cn\/how-to-realize-2.html","title":{"rendered":"C\u8bed\u8a00\u5982\u4f55\u5b9e\u73b0\u6cdb\u578b\u7f16\u7a0b\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6cdb\u578b\u7f16\u7a0b\u8ba9\u4f60\u7f16\u5199\u5b8c\u5168\u4e00\u822c\u5316\u5e76\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u7b97\u6cd5\uff0c\u5176\u6548\u7387\u4e0e\u9488\u5bf9\u67d0\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u800c\u8bbe\u8ba1\u7684\u7b97\u6cd5\u76f8\u540c\u3002\u5728 C \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u624b\u6bb5\u5b9e\u73b0\u8fd9\u6837\u7684\u6cdb\u578b\u7f16\u7a0b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u901a\u8fc7\u65e0\u7c7b\u578b\u6307\u9488 void*\u3002<\/p>\n

\u770b\u4e0b\u9762\u7684\u4e00\u4e2a\u5b9e\u73b0\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u5185\u5bb9\u7684\u51fd\u6570 swap\uff0c\u4ee5\u6574\u578b int \u4e3a\u4f8b\uff1a<\/p>\n

void swap(int* i1,int* i2){  \r\n         int temp;  \r\n         temp = *i1;  \r\n         *i1 = *i2;  \r\n         *i2 = temp;  \r\n}<\/pre>\n

\u5f53\u4f60\u60f3\u4ea4\u6362\u4e24\u4e2a char \u7c7b\u578b\u65f6\uff0c\u4f60\u8fd8\u5f97\u91cd\u5199\u4e00\u4e2a\u53c2\u6570\u7c7b\u578b\u4e3a char \u7684\u51fd\u6570\uff0c\u662f\u4e0d\u662f\u80fd\u7528\u65e0\u7c7b\u578b\u7684\u6307\u9488\u6765\u4f5c\u4e3a\u53c2\u6570\u5462\uff1f\u770b\u5982\u4e0b\u6539\u52a8\uff1a<\/p>\n

void swap(void *vp1,void *vp2){  \r\n       void temp = *vp1;  \r\n       *vp1 = *vp2;  \r\n       *vp2 = temp;  \r\n}<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff0c\u662f\u901a\u4e0d\u8fc7\u7f16\u8bd1\u7684\u3002\u9996\u5148\uff0c\u53d8\u91cf\u662f\u4e0d\u80fd\u58f0\u660e\u4e3a void \u65e0\u7c7b\u578b\u7684\u3002\u800c\u4f60\u4e0d\u77e5\u9053\u8c03\u7528\u6b64\u51fd\u6570\u4f20\u8fdb\u7684\u53c2\u6570\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\uff0c\u65e0\u6cd5\u786e\u5b9a\u4e00\u79cd\u7c7b\u578b\u7684\u58f0\u660e\u3002\u540c\u65f6\uff0c\u4e0d\u80fd\u5c06 * \u7528\u5728\u65e0\u7c7b\u578b\u6307\u9488\u4e0a\uff0c\u56e0\u4e3a\u7cfb\u7edf\u6ca1\u6709\u6b64\u5730\u5740\u6307\u5411\u5bf9\u8c61\u5927\u5c0f\u7684\u4fe1\u606f\u3002\u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5f97\u77e5\u4f20\u5165\u6b64\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u7684\u3002\u8fd9\u91cc\u8981\u60f3\u5b9e\u73b0\u6cdb\u578b\u7684\u51fd\u6570\uff0c\u9700\u8981\u5728\u8c03\u7528\u7684\u5730\u65b9\u4f20\u5165\u76f8\u5173\u8981\u4ea4\u6362\u7684\u5bf9\u8c61\u7684\u5730\u5740\u7a7a\u95f4\u5927\u5c0f size\uff0c\u540c\u65f6\u5229\u7528\u5728\u5934\u6587\u4ef6 string.h \u4e2d\u5b9a\u4e49\u7684 memcpy() \u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6539\u52a8\u5982\u4e0b\uff1a<\/p>\n

void swap(void *vp1,void *vp2,int size){  \r\n      char buffer[size];\/\/\u6ce8\u610f\u6b64\u5904gcc\u7f16\u8bd1\u5668\u662f\u5141\u8bb8\u8fd9\u6837\u58f0\u660e\u7684\r\n      memcpy(buffer,vp1,size);  \r\n      memcpy(vp1,vp2,size);  \r\n      memcpy(vp2,buffer,size);  \r\n}<\/pre>\n

\u5728\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u50cf\u5982\u4e0b\u8fd9\u6837\u8c03\u7528\uff08\u540c\u6837\u9002\u7528\u4e8e\u5176\u5b83\u7c7b\u578b\u7684 x\u3001y\uff09\uff1a<\/p>\n

int x = 27,y = 2;  \r\nswap(&x,&y,sizeof(int));<\/pre>\n

\u4e0b\u9762\u770b\u53e6\u4e00\u79cd\u529f\u80fd\u7684\u51fd\u6570\uff1a<\/p>\n

int lsearch(int key,int array[],int size){\r\n      for(int i = 0;i < size; ++i)\r\n                  if(array[i] == key)\r\n                           return i;\r\n     return -1;\r\n}<\/pre>\n

\u6b64\u51fd\u6570\u5728\u6570\u7ec4 array \u4e2d\u67e5\u627e key\u5143\u7d20\uff0c\u627e\u5230\u540e\u8fd4\u56de\u5b83\u7684\u7d22\u5f15\uff0c\u627e\u4e0d\u5230\u8fd4\u56de -1\u3002<\/p>\n

\u5982\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u6cdb\u578b\u7684\u51fd\u6570\uff1a<\/p>\n

void* lsearch(void* key, void *base, int n, int elemSize){\r\n    for(int i = 0;i < n; ++i){\r\n        void *elemAddr = (char *)base+i*elemSize;\r\n        if(memcmp(key, elemAddr, elemSize) == 0)\r\n            return elemAddr;\r\n    }\r\n    return NULL;\r\n}<\/pre>\n

\u4ee3\u7801\u7b2c\u4e09\u884c\uff1a\u5c06\u6570\u7ec4\u7684\u9996\u5730\u5740\u5f3a\u5236\u8f6c\u6362\u4e3a\u6307\u5411 char \u7c7b\u578b\u7684\u6307\u9488\uff0c\u662f\u5229\u7528 char \u7c7b\u578b\u5927\u5c0f\u4e3a1\u5b57\u8282\u7684\u7279\u6027\uff0c\u4f7f elemAddr \u6307\u5411\u6b64\"\u6cdb\u578b\"\u6570\u7ec4\u7684\u7b2c i-1 \u4e2a\u5143\u7d20\u7684\u9996\u5730\u5740\u3002\u56e0\u4e3a\u4e4b\u524d\u5df2\u7ecf\u8bf4\u8fc7\uff0c\u6b64\u65f6\u4f60\u5e76\u4e0d\u77e5\u9053\u4f60\u4f20\u5165\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\uff0c\u7cfb\u7edf\u65e0\u6cd5\u786e\u5b9a\u6b64\u6570\u7ec4\u4e00\u4e2a\u5143\u7d20\u6709\u591a\u957f\uff0c\u8df3\u5411\u4e0b\u4e2a\u5143\u7d20\u9700\u8981\u591a\u5c11\u5b57\u8282\uff0c\u6240\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e3a\u6307\u5411 char \u7684\u6307\u9488\uff0c\u518d\u52a0\u4e0a\u53c2\u6570\u4f20\u5165\u7684\u5143\u7d20\u5927\u5c0f\u4fe1\u606f\u548c\u7d2f\u52a0\u6570i\u7684\u4e58\u79ef\uff0c\u5373\u504f\u79fb\u5730\u5740\uff0c\u5373\u53ef\u5f97\u6b64\u6570\u7ec4\u7b2c i-1 \u4e2a\u5143\u7d20\u7684\u9996\u5730\u5740\u3002\u8fd9\u6837\u4f7f\u65e0\u8bba\u4f20\u5165\u7684\u53c2\u6570\u662f\u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u6307\u9488\uff0c\u90fd\u53ef\u4ee5\u5f97\u5230\u6307\u5411\u6b63\u786e\u5143\u7d20\u7684\u6307\u9488\uff0c\u5b9e\u73b0\u6cdb\u578b\u7f16\u7a0b\u3002<\/p>\n

\u51fd\u6570 memcmp() \u539f\u578b\uff1aint memcmp(void *dest,const void *src,int n)\uff0c\u6bd4\u8f83\u4e24\u6bb5\u957f\u5ea6\u4e3an\u9996\u5730\u5740\u5206\u522b\u4e3a dest\u3001src \u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n

\u6b64\u51fd\u6570\u5728\u6570\u7ec4 base \u4e2d\u67e5\u627e key \u5143\u7d20\uff0c\u627e\u5230\u5219\u8fd4\u56de\u5b83\u7684\u5730\u5740\u4fe1\u606f\uff0c\u627e\u4e0d\u5230\u5219\u8fd4\u56de NULL\u3002<\/p>\n

\u5982\u679c\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff0c\u5219\u53ef\u4ee5\u5b9e\u73b0\u5176\u884c\u4e3a\u7684\u6cdb\u578b\uff1a<\/p>\n

void *lsearch(void *key,void *base,int n,int elemSize,int(*cmpfn)(void*,void*,int)){\r\n    for(int i = 0;i < n; ++i){\r\n        void *elemAddr = (char *)base+i*elemSize;\r\n        if(cmpfn(key,elemAddr,elemSize) == 0)\r\n            return elemAddr;\r\n    }\r\n    return NULL;\r\n}<\/pre>\n

\u518d\u5b9a\u4e49\u4e00\u4e2a\u8981\u8c03\u7528\u7684\u51fd\u6570\uff1a<\/p>\n

int intCmp(void* elem1,void* elem2){\r\n        int* ip1 = elem1;\r\n        int* ip2 = elem2;\r\n        return *ip1-*ip2;\r\n}<\/pre>\n

\u770b\u5982\u4e0b\u8c03\u7528\uff1a<\/p>\n

int array[] = {1,2,3,4,5,6};\r\nint size = 6;\r\nint number = 3;\r\nint *found = lsearch(&number,array,size,sizeof(int),intCmp);\r\nif(found == NULL)\r\n         printf(\"NO\\n\");\r\nelse\r\n         printf(\"YES\\n\");<\/pre>\n

C \u8bed\u8a00\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e00\u5b9a\u7684\u6cdb\u578b\u7f16\u7a0b\uff0c\u4f46\u8fd9\u6837\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u7cfb\u7edf\u5bf9\u5176\u53ea\u6709\u6709\u9650\u7684\u68c0\u67e5\u3002\u5728\u7f16\u7a0b\u65f6\u4e00\u5b9a\u8981\u591a\u52a0\u7ec6\u5fc3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u901a\u8fc7\u65e0\u7c7b\u578b\u6307\u9488 void*\u3002 \u770b\u4e0b\u9762\u7684\u4e00\u4e2a\u5b9e\u73b0\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u5185\u5bb9\u7684\u51fd\u6570 swap\uff0c\u4ee5\u6574\u578b i […]<\/p>\n","protected":false},"author":321,"featured_media":214737,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-214736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/214736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=214736"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/214736\/revisions"}],"predecessor-version":[{"id":214744,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/214736\/revisions\/214744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/214737"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=214736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=214736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=214736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}