{"id":403,"date":"2023-02-25T10:48:54","date_gmt":"2023-02-25T02:48:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=403"},"modified":"2023-04-29T21:37:16","modified_gmt":"2023-04-29T13:37:16","slug":"android-ndk-basic-c-pointer-and-arrays-function-pointer","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-c-pointer-and-arrays-function-pointer\/","title":{"rendered":"Android NDK\u57fa\u78403\uff1aC_\u6307\u9488\u4e0e\u6570\u7ec4_\u51fd\u6570\u6307\u9488"},"content":{"rendered":"<h2>\u6307\u9488\u4e0e\u6570\u7ec4<\/h2>\n<pre><code class=\"language-c\">\/\/1.\u901a\u8fc7\u6307\u9488\u7ed9\u6570\u7ec4\u8d4b\u503c\nvoid main() {\n    int uids[5];\n    \/\/\u9ad8\u7ea7\u5199\u6cd5\n    \/\/int i = 0;\n    \/\/for (; i &lt; 5; i++) {\n    \/\/    uids[i] = i;\n    \/\/}\n    \/\/\u65e9\u4e9b\u7248\u672c\u7684\u5199\u6cd5\n    int* p = uids;\n    printf(&quot;%#x\\n&quot;, p);\n    int i = 0; \/\/i\u662f\u6570\u7ec4\u5143\u7d20\u7684\u503c\n    for (; p &lt; uids + 5; p++) {\n        *p = i;\n        i++;\n    }\n\n    getchar();\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-c\">\/\/2.\u6307\u9488\u4e0e\u6570\u7ec4\u7684\u51e0\u79cd\u5199\u6cd5\nvoid main() {\n    int a[] = { 78, 34, 73, 25, 80, 90 };\n    int i = 0;\n    for (; i &lt; 6; i++) {\n        printf(&quot;%d, %#x&quot;, a[i], &amp;a[i]);\n        \/\/a\u9996\u5730\u5740\n        \/\/a \u7b49\u4ef7\u4e8e&amp;a[0]\uff0ca+1 \u7b49\u4ef7\u4e8e&amp;a[1]\n        \/\/\u7ed3\u8bba\uff1a a+i \u7b49\u4ef7\u4e8e &amp;a[i]\n        \/\/*a\u7b49\u4ef7\u4e8e a[0]\uff0c*(a+1)\u7b49\u4ef7\u4e8ea[1]\n        \/\/\u7ed3\u8bba\uff1a *(a+i)\u7b49\u4ef7\u4e8ea[i]\n        printf(&quot;%d, %#x&quot;, *(a+i), a+i);\n        printf(&quot;\\n&quot;);\n    }\n    \/\/\u603b\u7ed3\uff1a\n    \/\/a+i \u7b49\u4ef7\u4e8e &amp;a[i]\uff0c*(a+i)\u7b49\u4ef7\u4e8ea[i]\uff0c\u540e\u8005\u53ea\u662f\u7b80\u5199\u65b9\u5f0f\uff0c\u5728\u6700\u53e4\u8001\u7684C\u8bed\u8a00\u91cc\u662f\u6ca1\u6709\u4e2d\u62ec\u53f7\u7684\n\n    \/\/\u53e6\u5916\u4e00\u79cd\u5199\u6cd5\n    printf(&quot;\\n\\n&quot;);\n    int* p = a;\n    \/\/p\u53d8\u91cf\u6307\u9488\uff0ca\u5e38\u91cf\u6307\u9488\n    \/\/p = &amp;i;\n    \/\/a = &amp;i; \u4e0d\u884c\n    i = 0;\n    for (; i &lt; 6; i++) {\n        \/\/a[i]\u8868\u8fbe\u5f0f\uff1a\u9996\u5730\u5740[i]\n        printf(&quot;%d, %#x&quot;, p[i], &amp;p[i]);\n        printf(&quot;%d, %#x&quot;, *(p+i), p+i);\n        printf(&quot;\\n&quot;);\n    }\n\n    getchar();\n}\n\n\/\/\u6307\u9488\u5f15\u7528\u4e8c\u7ef4\u6570\u7ec4\uff08\u884c\u6307\u9488\uff0c\u5217\u6307\u9488\uff09\nvoid main() {\n    int a[2][3] = {95,82,56,17,29,30};\n    \/\/\u904d\u5386\u8f93\u51fa\n    \/\/\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\uff0c\u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\n    int i = 0;\n    for (; i &lt; 2; i++){\n        int j = 0;\n        for (; j &lt; 3; j++){\n            printf(&quot;%d, %#x&quot;, a[i][j], &amp;a[i][j]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n\n    \/\/\n    \/\/printf(&quot;%#x,%#x,%#x\\n&quot;, a, &amp;a, *a);\n    \/\/printf(&quot;%d,%d,%d\\n&quot;, sizeof(*a), sizeof(*&amp;a), sizeof(**a));\n    \/\/a\u662f\u4e00\u4e2a\u884c\u6307\u9488\uff0c\u6307\u5411\u4e00\u4e2a\u6709\u4e09\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c12\n    \/\/&amp;a\u662f\u4e00\u4e2a\u6307\u5411\u4e8c\u7ef4\u6570\u7ec4\u7684\u6307\u9488\uff0c\u5f53\u524d\u4e8c\u7ef4 \u6570\u7ec46\u4e2a\u5143\u7d20\uff0c24\n    \/\/*a \u662f\u4e00\u4e2a\u6307\u5411int\u7c7b\u578b\u6570\u636e\u7684\u6307\u9488\uff08a[0][0]\uff09\uff0c4\n    \/\/printf(&quot;%d\\n&quot;, **a);\n\n    \/\/a\u662f\u4e00\u4e2a\u884c\u6307\u9488\uff0c\u662f\u6570\u7ec4\u7b2c\u4e00\u884c\u7684\u6307\u9488\uff0ca+1\u7b2c\u4e8c\u884c\u7684\u6307\u9488\uff0c\u4ee5\u6b64\u7c7b\u63a8\n    printf(&quot;%#x,%#x\\n&quot;, a, a+1);\n    \/\/*a\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u884c\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\uff0c*a+1\u662f\u6570\u7ec4\u7b2c\u4e00\u884c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u6307\u9488\n    printf(&quot;%#x,%#x\\n&quot;, *a, *a+1);\n    \/\/*(a+1)\u662f\u6570\u7ec4\u7684\u7b2c\u4e8c\uff081\uff09\u884c\u7b2c\u4e00(0)\u4e2a\u5143\u7d20\u7684\u6307\u9488\n    \/\/\u6ce8\u610f\uff1a\u4e0d\u4e00\u5b9a\u662f\u6b63\u5e38\u903b\u8f91\n    printf(&quot;%#x,%#x\\n&quot;, *(a+1), *(a+1)+1);\n\n    \/\/\u53d6\u6570\u7ec4\u7684\u7b2c\u4e8c\uff081\uff09\u884c\uff0c\u7b2c\u4e09\uff082\uff09\u4e2a\u5143\u7d20\n    printf(&quot;%d\\n&quot;, a[1][2]);\n    printf(&quot;%d\\n&quot;, *(*(a+1)+2));\n\n    \/\/\u603b\u7ed3\uff1a\n    \/\/a[i][j] \u7b49\u4ef7\u4e8e *(*(a+i)+j)\n    \/\/&amp;a[i][j] \u7b49\u4ef7\u4e8e (*(a+i)+j)\n\n    getchar();\n}<\/code><\/pre>\n<h2>\u51fd\u6570\u6307\u9488<\/h2>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;Windows.h&gt;\n\n\/\/3.\u51fd\u6570\u6307\u9488\nint msg(char* msg, char* title) {\n    MessageBox(0, msg, title, 0);\n    return 0;\n}\n\nvoid main() {\n    \/\/msg();\n    \/\/msg\u548c&amp;msg\u90fd\u662fmsg\u51fd\u6570\u5730\u5740\n    printf(&quot;%#x\\n&quot;, msg);\n    printf(&quot;%#x\\n&quot;, &amp;msg);\n    \/\/\u51fd\u6570\u6307\u9488\n    \/\/\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u51fd\u6570\u6307\u9488\u7684\u540d\u79f0\uff0c\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\n    int(*fun_p)(char* msg, char* title) = msg;\n    fun_p(&quot;\u6d88\u606f\u5185\u5bb9&quot;, &quot;\u6807\u9898&quot;);\n\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">int add(int a, int b) {\n    return a + b;\n}\n\nint minus(int a, int b) {\n    return a - b;\n}\n\n\/\/compute\u51fd\u6570\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u53c2\u6570\n\/\/\u7c7b\u4f3c\u4e8e\u6211\u4eecJava\u4e2d\u7684\u56de\u8c03\u51fd\u6570\nvoid compute(int(*func_p)(int a, int b), int m, int n) {\n    printf(&quot;\u6267\u884c\u4e00\u6bb5\u4ee3\u7801...\\n&quot;);\n    printf(&quot;\u6267\u884c\u56de\u8c03\u51fd\u6570...\\n&quot;);\n    int r = func_p(m, n);\n    printf(&quot;\u6267\u884c\u7ed3\u679c\uff1a%d\\n&quot;, r);\n}\n\nvoid main() {\n    \/\/\u52a0\u6cd5\n    \/\/int(*func_p)(int a, int b) = add;\n    compute(add, 10, 20);\n    \/\/\u51cf\u6cd5\n    \/\/compute(minus, 50, 10);\n    getchar();\n}<\/code><\/pre>\n<h2>\u7efc\u5408\u6848\u4f8b<\/h2>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;Windows.h&gt;\n#include &lt;math.h&gt;\n#include &lt;time.h&gt;\n\n\/\/\u6848\u4f8b\uff1a\u7528\u968f\u673a\u6570\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5199\u4e00\u4e2a\u51fd\u6570\u67e5\u627e\u6700\u5c0f\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u6700\u5c0f\u6570\u7684\u5730\u5740\uff0c\u5728\u4e3b\u51fd\u6570\u4e2d\u6253\u5370\u51fa\u6765\nint* getMinPointer(int ids[], int len) {\n    int i = 0;\n    int* p = &amp;ids[0];\n    for (; i &lt; len; i++) {\n        if (ids[i] &lt; *p) {            \n            p = &amp;ids[i];\n        }\n    }\n    return p;\n}\n\nvoid main() {\n    int ids[10];\n    int i = 0;\n    \/\/\u521d\u59cb\u5316\u968f\u673a\u6570\u53d1\u751f\u5668\uff0c\u8bbe\u7f6e\u79cd\u5b50\uff0c\u79cd\u5b50\u4e0d\u4e00\u6837\uff0c\u968f\u673a\u6570\u624d\u4e0d\u4e00\u6837\n    \/\/\u5f53\u524d\u65f6\u95f4\u4f5c\u4e3a\u79cd\u5b50 \u6709\u7b26\u53f7 int -xx - &gt; +xx\n    srand((unsigned) time(NULL));\n    for (; i &lt; 10; i++) {\n        \/\/100\u8303\u56f4\u5185\n        ids[i] = rand() % 100;\n        printf(&quot;%d\\n&quot;, ids[i]);\n    }\n\n    int* p = getMinPointer(ids, sizeof(ids) \/ sizeof(int));\n    printf(&quot;%#x,%d\\n&quot;, p, *p);\n    getchar();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6307\u9488\u4e0e\u6570\u7ec4 \/\/1.\u901a\u8fc7\u6307\u9488\u7ed9\u6570\u7ec4\u8d4b\u503c void main() { int uids[5]; \/\/\u9ad8\u7ea7\u5199\u6cd5 \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=403"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}