{"id":211,"date":"2020-08-31T10:04:54","date_gmt":"2020-08-31T10:04:54","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=211"},"modified":"2020-08-31T10:04:54","modified_gmt":"2020-08-31T10:04:54","slug":"c-pointers","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/","title":{"rendered":"C++ Pointers"},"content":{"rendered":"<p>In this lesson, we would learn about C++ pointers. This is one of the few topics that is distinguishes C++ from many other programming languages. So, it&#8217;s important you understand it.<\/p>\n<ol>\n<li><a href=\"#t1\">Overview of C++ Pointers<\/a><\/li>\n<li><a href=\"#2\">Creating Pointers<\/a><\/li>\n<li><a href=\"#t3\">Using Pointers in C++<\/a><\/li>\n<li><a href=\"#t4\">Some Pointer Concepts<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Overview of C++ Pointers<\/strong><\/h4>\n<p>Pointers in C++ are variables that store memory address of other variables. So if you have a variable x, then to get its memory address, you use &amp;x. The code below displays address of three variables<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Printing memory addresses of variables<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n    <span style=\"color: #888888;\">\/\/ declare some variables<\/span>\r\n    <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">67<\/span>;\r\n    string name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>;\r\n    <span style=\"color: #333399; font-weight: bold;\">double<\/span> y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">54.32<\/span>;\r\n\r\n    <span style=\"color: #888888;\">\/\/ print address of x<\/span>\r\n    cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Address of x: \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">&amp;<\/span>x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n    <span style=\"color: #888888;\">\/\/ print address of name<\/span>\r\n    cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Address of name: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">&amp;<\/span>name <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n    <span style=\"color: #888888;\">\/\/ print address of y<\/span>\r\n    cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Address of y: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">&amp;<\/span>y <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>The output of the program is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Address of x: 0x62fe08\r\nAddress of name: 0x62fde0\r\nAddress of y: 0x62fdd8\r\n<\/pre>\n<p>As you can see, the addressed are shown in hexadecimal (begins with 0x)<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Creating Pointers<\/strong><\/h4>\n<p>Similar to normal variables, you also need to declare a pointer before you can use it. However, pointer names are preceded by an asterisk(*). For example<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">*<\/span>myPointer; <span style=\"color: #888888;\">\/\/pointer to int<\/span>\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">*<\/span> myPointer; <span style=\"color: #888888;\">\/\/pointer to int<\/span>\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">*<\/span> myPointer, myVar; <span style=\"color: #888888;\">\/\/a pointer and an int<\/span>\r\n<\/pre>\n<p>The three lines above are all valid ways to declare a pointer. The first and second lines declares a pointer <em>myPointer<\/em> which is a pointer to an int variable. Third line declares a pointer <em>myPointer<\/em> and a normal variable <em>myVar<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Using Pointers in C++<\/strong><\/h4>\n<p>Let&#8217;s now create a pointer and assign the address of a variable to it.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Working with point<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n    <span style=\"color: #888888;\">\/\/ declare a variables, x<\/span>\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">34<\/span>;\r\n\r\n\t<span style=\"color: #888888;\">\/\/ declare a pointer px<\/span>\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">*<\/span>px;\r\n\r\n\t<span style=\"color: #888888;\">\/\/ assign the pointer a value<\/span>\r\n\tpx <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">&amp;<\/span>x;\r\n\r\n\t<span style=\"color: #888888;\">\/\/ display the pointer value<\/span>\r\n\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Variable x \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\" is stored at \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> px;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>The output is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Variable x 34 is stored at 0x62fe14\r\n<\/pre>\n<p>Moreover, if you have a pointer and you want to know what value is stored there, you just also use *. The code below would get the value stored in px.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">cout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Value stored in \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> px <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\" is \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">*<\/span>px;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Some Pointer Concepts<\/strong><\/h4>\n<p>There are a few concepts on pointers I would like to mention here finally.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Concept &amp; Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>Null Pointers<\/strong>A null pointer is is a constant with a value of zero defined in several standard libraries.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>Pointer Arithmetic<\/strong>Also, you can also perform arithmetic operations with pointers. The four arithmetic operators that you can use on pointers are: ++, &#8211;, +, &#8211;<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>Pointers vs Arrays<\/strong>There is a relationship between pointers and arrays. We&#8217;ll explore this in later lessons<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>Array Pointers<\/strong>You can define arrays to hold a number of pointers.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>Pointer to Pointer<\/strong>C++ allows you to have pointer holding the memory address of another pointer.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><strong>Passing Pointers to Functions<\/strong>Passing arguments by reference or by address both allow for the passed argument to be modified in the calling function by the called function.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>Returning Pointer from Functions<\/strong>You can also allows to function to return a pointer to local variable, static variable as well as\u00a0 dynamically allocated memory.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"pre-btn\"><\/div>\n<div>I recommend you ensure to understand clearly the concept of pointers in C++. Get more support from my <a href=\"https:\/\/www.youtube.com\/kindsonthetechpro\" target=\"_blank\" rel=\"noopener noreferrer\">YouTube Channel, Kindson The Tech Pro.<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would learn about C++ pointers. This is one of the few topics that is distinguishes C++ from many other programming languages. &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[32],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-pointers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Pointers - C++ Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Pointers - C++ Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this lesson, we would learn about C++ pointers. This is one of the few topics that is distinguishes C++ from many other programming languages. &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/\" \/>\n<meta property=\"og:site_name\" content=\"C++ Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-31T10:04:54+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"headline\":\"C++ Pointers\",\"datePublished\":\"2020-08-31T10:04:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/\"},\"wordCount\":411,\"commentCount\":1,\"keywords\":[\"Pointers\"],\"articleSection\":[\"C++ Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/\",\"name\":\"C++ Pointers - C++ Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\"},\"datePublished\":\"2020-08-31T10:04:54+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-pointers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Pointers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\",\"name\":\"C++ Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Pointers - C++ Tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/","og_locale":"en_US","og_type":"article","og_title":"C++ Pointers - C++ Tutorials","og_description":"In this lesson, we would learn about C++ pointers. This is one of the few topics that is distinguishes C++ from many other programming languages. &hellip;","og_url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/","og_site_name":"C++ Tutorials","article_published_time":"2020-08-31T10:04:54+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"headline":"C++ Pointers","datePublished":"2020-08-31T10:04:54+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/"},"wordCount":411,"commentCount":1,"keywords":["Pointers"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/","url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/","name":"C++ Pointers - C++ Tutorials","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website"},"datePublished":"2020-08-31T10:04:54+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-pointers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/cplusplus\/"},{"@type":"ListItem","position":2,"name":"C++ Pointers"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website","url":"https:\/\/kindsonthegenius.com\/cplusplus\/","name":"C++ Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/cplusplus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/cplusplus\/author\/kindsonthegenius-3\/"}]}},"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}