{"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:\/\/www.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":{"footnotes":""},"categories":[2],"tags":[32],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-pointers"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/211\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}