{"id":521,"date":"2013-07-26T15:22:48","date_gmt":"2013-07-26T22:22:48","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=521"},"modified":"2013-07-26T17:01:34","modified_gmt":"2013-07-27T00:01:34","slug":"c-pointer","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=521","title":{"rendered":"C Pointer"},"content":{"rendered":"<p>\n\tPointer Related: Array, Structure, Single Pointer (*), Double Pointer (**), Reference (&amp;)\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<p>\n\t<span style=\"font-size:20px;\">1-D Array: integer<\/span>\n<\/p>\n<pre class=\"brush:cpp;\">\r\nint a[5] = {10,20,30,40,50};<\/pre>\n<p>\n\tOutput\n<\/p>\n<p>\n\ta[0] = 10 &nbsp;&nbsp; &nbsp; &amp;a[0] = 0x5FBFF8E0 &nbsp;&nbsp; &nbsp; a + 0 = 0x5FBFF8E0<br \/>\n\ta[1] = 20 &nbsp;&nbsp; &nbsp; &amp;a[1] = 0x5FBFF8E4 &nbsp;&nbsp; &nbsp; a + 1 = 0x5FBFF8E4<br \/>\n\ta[2] = 30 &nbsp;&nbsp; &nbsp; &amp;a[2] = 0x5FBFF8E8 &nbsp;&nbsp; &nbsp; a + 2 = 0x5FBFF8E8<br \/>\n\ta[3] = 40 &nbsp;&nbsp; &nbsp; &amp;a[3] = 0x5FBFF8EC &nbsp;&nbsp; &nbsp; a + 3 = 0x5FBFF8EC<br \/>\n\ta[4] = 50 &nbsp;&nbsp; &nbsp; &amp;a[4] = 0x5FBFF8F0 &nbsp;&nbsp; &nbsp; a + 4 = 0x5FBFF8F0\n<\/p>\n<p>\n\t\u200b<a href=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/1D_Array.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"1D_Array\" class=\"alignnone size-full wp-image-524\" height=\"71\" src=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/1D_Array.png\" width=\"665\" srcset=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/1D_Array.png 665w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/1D_Array-300x32.png 300w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/1D_Array-500x53.png 500w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/a>\n<\/p>\n<p>\n\t<span style=\"font-size:20px;\">2-D Array: integer<\/span>\n<\/p>\n<pre class=\"brush:cpp;\">\r\nint a[3][2] = {10,20,30,40,50,60};\r\n\/\/ or\r\nint a[3][2] = { {10,20},{30,40},{50,60}};\r\n\/\/ or\r\nint **a; \r\n\/*\r\n    **a = 10, *(*a+1) = 20\r\n    **(a+1) = 30, *(*(a+1)+1) = 40\r\n*\/\r\n<\/pre>\n<p>\n\t<span style=\"color:#FF0000;\">a[x][y] = *(a[x] + y) = *( *(a+x) + y&nbsp;)<\/span>\n<\/p>\n<p>\n\tOutput\n<\/p>\n<p>\n\ta[0][0] = 10 &nbsp;&nbsp; &nbsp; a[0] + 0 = 0x5FBFF8E0 &nbsp; ; &nbsp; a[0][1] = 20 &nbsp;&nbsp; &nbsp; a[0] + 1 = 0x5FBFF8E4&nbsp;<br \/>\n\ta[1][0] = 30 &nbsp;&nbsp; &nbsp; a[1] + 0 = 0x5FBFF8E8 &nbsp; ; &nbsp; a[1][1] = 40 &nbsp;&nbsp; &nbsp; a[1] + 1 = 0x5FBFF8EC&nbsp;<br \/>\n\ta[2][0] = 50 &nbsp;&nbsp; &nbsp; a[2] + 0 = 0x5FBFF8F0 &nbsp; ; &nbsp; a[2][1] = 60 &nbsp;&nbsp; &nbsp; a[2] + 1 = 0x5FBFF8F4&nbsp;<br \/>\n\ta[0] = 0x5FBFF8E0 &nbsp;&nbsp; &nbsp; a + 0 = 0x5FBFF8E0 &nbsp;&nbsp; &nbsp; *(a+0) = 0x5FBFF8E0<br \/>\n\ta[1] = 0x5FBFF8E8 &nbsp;&nbsp; &nbsp; a + 1 = 0x5FBFF8E8 &nbsp;&nbsp; &nbsp; *(a+1) = 0x5FBFF8E8<br \/>\n\ta[2] = 0x5FBFF8F0 &nbsp;&nbsp; &nbsp; a + 2 = 0x5FBFF8F0 &nbsp;&nbsp; &nbsp; *(a+2) = 0x5FBFF8F0\n<\/p>\n<p>\n\t<a href=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/2D_Array.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"2D_Array\" class=\"alignnone size-full wp-image-528\" height=\"196\" src=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/2D_Array.png\" width=\"618\" srcset=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/2D_Array.png 618w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/2D_Array-300x95.png 300w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/2D_Array-500x158.png 500w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a>\n<\/p>\n<p>\n\t<span style=\"font-size:20px;\">char * vs. char []<\/span>\n<\/p>\n<pre class=\"brush:cpp;\">\r\nchar ch1[] = &quot;abcde&quot;;\r\nprintf(&quot;0x%X 0x%X \\t %s\\n&quot;,ch1,&amp;ch1,ch1);\r\n    \r\nchar *ch2 = &quot;abcde&quot;;\r\nprintf(&quot;0x%X 0x%X \\t %s\\n&quot;,ch2,&amp;ch2,ch2);<\/pre>\n<p>\n\tOutput\n<\/p>\n<p>\n\tch1(%X) = 0x5FBFF8F2 &nbsp; &amp;ch1 = 0x5FBFF8F2 &nbsp; &nbsp; &nbsp;ch1(%s) = abcde<br \/>\n\tch2<span style=\"font-size: 13px; \">(%X)<\/span>&nbsp;= 0xE37 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;ch2 = 0x5FBFF8F0 &nbsp; &nbsp; &nbsp;ch2<span style=\"font-size: 13px; \">(%s) =&nbsp;<\/span>abcde\n<\/p>\n<p>\n\t<a href=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/char-array.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"char array\" class=\"alignnone size-full wp-image-537\" height=\"279\" src=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/char-array.png\" width=\"698\" srcset=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/char-array.png 698w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/char-array-300x119.png 300w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/07\/char-array-500x199.png 500w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/a>\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\tStructure\n<\/p>\n<pre class=\"brush:cpp;\">\r\nstruct student\r\n{\r\n    char firstname[20];\r\n    char *lastname;\r\n    int score;\r\n};\r\nstruct student s1 = {&quot;Carlos&quot;, &quot;Wang&quot;, 100};\r\nprintf(&quot;%s %s = %d\\n&quot;,s1.firstname,s1.lastname,s1.score);\r\nprintf(&quot;&amp;s1 = 0x%X\\n&quot;,&amp;s1);\r\n\r\nprintf(&quot;&amp;s1.firstname = 0x%X \\t s1.firstname(%%X) = 0x%X \\t s1.firstname(%%s) = %s\\n&quot;,&amp;s1.firstname,s1.firstname, s1.firstname);\r\n\r\nprintf(&quot;&amp;s1.lastname = 0x%X \\t s1.lastname(%%X) = 0x%X \\t s1.lastname(%%s) = %s\\n&quot;,&amp;s1.lastname,s1.lastname, s1.lastname);\r\n<\/pre>\n<p>\n\tOutput\n<\/p>\n<p>\n\tCarlos Wang = 100<br \/>\n\t&amp;s1 = 0x5FBFF8D0<br \/>\n\t&amp;s1.firstname = 0x5FBFF8D0 &nbsp;&nbsp; &nbsp; s1.firstname(%X) = 0x5FBFF8D0 &nbsp;&nbsp; &nbsp; s1.firstname(%s) = Carlos<br \/>\n\t&amp;s1.lastname = 0x5FBFF8E8 &nbsp;&nbsp; &nbsp; s1.lastname(%X) = 0xE1D &nbsp;&nbsp; &nbsp; s1.lastname(%s) = Wang\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pointer Related: Array, Structure, Single Pointer (*), Double Pointer (**), Reference (&amp;)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-cc"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=521"}],"version-history":[{"count":14,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":526,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions\/526"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}