{"id":1883,"date":"2018-12-27T12:00:00","date_gmt":"2018-12-27T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/basics-of-pointers-in-c-a-simple-tutorial\/"},"modified":"2026-07-05T03:22:01","modified_gmt":"2026-07-05T01:22:01","slug":"basics-of-pointers-in-c-a-simple-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/basics-of-pointers-in-c-a-simple-tutorial\/","title":{"rendered":"Basics of Pointers in C++ (A Simple Tutorial)"},"content":{"rendered":"<p>Pointers in C++ is one of the core concepts that you need to learn. This is because it is a feature that helps you understand how your program code interacts with memory. So let&#8217;s get started.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-289 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Pointers-in-C-300x203.jpg\" alt=\"\" width=\"300\" height=\"203\" \/><\/p>\n<ol>\n<li><a href=\"#t1\">What are Pointer?<\/a><\/li>\n<li><a href=\"#t2\">Declaring and Assigning a Pointer<\/a><\/li>\n<li><a href=\"#t3\">Accessing Value in a Pointer (Dereferencing)<\/a><\/li>\n<li><a href=\"#t4\">The Complete Program<\/a><\/li>\n<li><a href=\"#video\">Watch the Video Tutorial<\/a><\/li>\n<\/ol>\n<p><strong id=\"t1\">1. What are Pointer?<\/strong><\/p>\n<p>A pointer is simply the address of a variable in memory. Let&#8217;s clarify this. When you assign a variable in C++, what you are doing is creating a location in memory and placing a value in there. For example, if you say<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #333399; font-weight: bold;\">int<\/span> num <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">45<\/span>;\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The number 45 is stored in a memory location which out program labels num. However the computer keeps the address of this memory location. So a pointer simply keeps the address of memory locations. To view this address, we would use the ampersand (&amp;) symbol with the name of the variable.\u00a0 The code below displays the address of num<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #333399; font-weight: bold;\">int<\/span> num <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">45<\/span>;\n  cout<span style=\"color: #333333;\">&lt;&lt;&amp;<\/span>num;\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The above code would display:\u00a00x61ff1c<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Declaring and Assigning a Pointer<\/strong><\/p>\n<p>Now assuming we want to store this address in some variable. We cannot store it on just normal variables. We can only store it in pointer variables. A pointer variable (or just pointer) is used to hold addresses of other variable. Before you can use a pointer variable, you must declare it like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">*<\/span>np;\t\t<span style=\"color: #888888;\">\/\/Declare a pointer<\/span>\nnp <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">&amp;<\/span>num;\t\t<span style=\"color: #888888;\">\/\/Assign a value to the pointer<\/span>\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This code\u00a0 would declares a pointer np. Simply by adding an asterisk (*) to the variable, we are declaring it as a pointer. So when we want to use it we could just use it without the\u00a0 *.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id =\"t3\">3. Accessing the Value in a Pointer<\/strong><\/p>\n<p>Just as we mentioned before, a pointer is an address. So if we have a pointer, we could access the value stored in this address. To do this, we use the dereferencing operator(which is also an asterisk) with the name of the pointer.<\/p>\n<p>The code below would give us the actual value stored in the pointer.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"> cout<span style=\"color: #333333;\">&lt;&lt;*<\/span>np;\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong id = \"t4\">The Complete Program<\/strong><\/p>\n<p>I have made a complete program with a line by line explanation of how to work with pointers in C++. Feel free to copy this program and run it to make sure you are clear with how pointers work.\u00a0 In\u00a0 a different lesson, we would examine pointers to pointers.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/============================================================================<\/span>\n<span style=\"color: #888888;\">\/\/ Name        : Pointers Tutorial in C++<\/span>\n<span style=\"color: #888888;\">\/\/ Author      : Kindson The Tech Pro<\/span>\n<span style=\"color: #888888;\">\/\/ Copyright   : Your copyright notice<\/span>\n<span style=\"color: #888888;\">\/\/ Date \t   : 28th December, 2018<\/span>\n<span style=\"color: #888888;\">\/\/============================================================================<\/span>\n\n<span style=\"color: #557799;\">#include &lt;stdio.h&gt;<\/span>\n<span style=\"color: #557799;\">#include &lt;stdlib.h&gt;<\/span>\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\n\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>(<span style=\"color: #333399; font-weight: bold;\">void<\/span>) {\n\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> num <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">45<\/span>;\t<span style=\"color: #888888;\">\/\/Declare an int variable num and assing a value of 45<\/span>\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">*<\/span>np;\t\t<span style=\"color: #888888;\">\/\/Declare a pointer(address of an int variable)<\/span>\n\tnp <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">&amp;<\/span>num;\t\t<span style=\"color: #888888;\">\/\/Assign an address to the pointer<\/span>\n\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span>np<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\t<span style=\"color: #888888;\">\/\/Display the pointer(address)<\/span>\n\tcout<span style=\"color: #333333;\">&lt;&lt;*<\/span>np;\t\t<span style=\"color: #888888;\">\/\/Display the value in the address(dereferencing)<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\n}\n<\/pre>\n<\/div>\n<p>If you run this program correctly, then your output would be as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-288 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Pointers-in-C-Output-1024x673.jpg\" alt=\"\" width=\"735\" height=\"483\" \/><\/p>\n<p><b id =\"video\"> Watch the Video<\/b><br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/fHm-nvtkWxA\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><!--more--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pointers in C++ is one of the core concepts that you need to learn. This is because it is a feature that helps you understand &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1883","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1883"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1883\/revisions"}],"predecessor-version":[{"id":2051,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1883\/revisions\/2051"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}