{"id":36,"date":"2008-01-25T22:47:09","date_gmt":"2008-01-26T02:47:09","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=36"},"modified":"2008-01-25T22:51:54","modified_gmt":"2008-01-26T02:51:54","slug":"c-pointers-and-references","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=36","title":{"rendered":"C++ Pointers and References"},"content":{"rendered":"<p>Pointers<\/p>\n<ul>\n<li>Why pointers\n<ul>\n<li>Faster array operation<\/li>\n<li>Access large blocks of data out of functions<\/li>\n<li>Allocate memory space dynamically at runtime<\/li>\n<\/ul>\n<\/li>\n<li>Declare and initialize a pointer\n<ul>\n<li>long* pnumber = NULL;<\/li>\n<li>long* pnumber2 = &amp;number2;<\/li>\n<\/ul>\n<\/li>\n<li>Assign memory address to a pointer\n<ul>\n<li>Using the address-of operator (&amp;)<\/li>\n<li>pnumber = &amp;number;<\/li>\n<\/ul>\n<\/li>\n<li>Obtain value pointed to by a pointer\n<ul>\n<li>Use de-reference operator (*)<\/li>\n<li>*pnumber++;<\/li>\n<\/ul>\n<\/li>\n<li>A pointer should ALWAYS be initialized. Otherwise, it might contains random value.<\/li>\n<li>Example\n<ul><code lang=\"cpp\"><br \/>\nint* pnum1 = NULL;<br \/>\nint num1 = 10;<br \/>\npnum1 = &amp; num1;<br \/>\n++*pnum1;<br \/>\n*pnum1 += 10<br \/>\ncout &lt;&lt; \"num1 is now: \" &lt;&lt; num1 &lt;&lt; endl;<br \/>\n\/\/ num1 should be 21<br \/>\n<\/code><\/ul>\n<\/li>\n<li>Pointers to char\n<ul>\n<li>char* can be initialized with a string literal<\/li>\n<li>char* pproverb = &#8220;A miss is as good as a mile&#8221;;\n<ul>\n<li>Ends with \\0<\/li>\n<li>\/\/ Print out the string<\/li>\n<li>cout &lt;&lt; pprover &lt;&lt; endl;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>References<\/p>\n<ul>\n<li>A reference is an alias for another variable\n<ul>\n<li>so the variable it&#8217;s referencing needs to be declared first<\/li>\n<li>a reference can&#8217;t be reassigned to another variable<\/li>\n<\/ul>\n<\/li>\n<li>Declare and initialize\n<ul>\n<li>long number = 0;<\/li>\n<li>long&amp; rnumber = number; \/\/ rnumber is an alias to number now<\/li>\n<li>rnumber += 10; \/\/ same as number += 10;<\/li>\n<\/ul>\n<\/li>\n<li>If we use pointer then\n<ul>\n<li>long number = 0;<\/li>\n<li>long* pnumber = &amp;number; \/\/ pnumber stores the address of number variable<\/li>\n<li>*pnumber += 10; \/\/ pnumber has to be de-referenced<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Pointers Why pointers Faster array operation Access large blocks of data out of functions Allocate memory space dynamically at runtime Declare and initialize a pointer long* pnumber = NULL; long* pnumber2 = &amp;number2; Assign memory address to a pointer Using &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=36\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"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,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-cpp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-A","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}