{"id":213,"date":"2020-08-31T11:28:23","date_gmt":"2020-08-31T11:28:23","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=213"},"modified":"2020-08-31T11:29:28","modified_gmt":"2020-08-31T11:29:28","slug":"c-references","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-references\/","title":{"rendered":"C++ References"},"content":{"rendered":"<p>The concepts of references\u00a0 is one of the key concepts in programming. This is regardless of the programming language. So ensure you understand it since is quite easy and clear. Also since it relates to pointer, it&#8217;s better you go over <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-pointers\/\" target=\"_blank\" rel=\"noopener noreferrer\">C++ Pointers<\/a> first before you continue.<\/p>\n<ol>\n<li><a href=\"#t1\">What are References<\/a><\/li>\n<li><a href=\"#t2\">Creating References in C++<\/a><\/li>\n<li><a href=\"#t3\">Passing Parameters by Reference<\/a><\/li>\n<li><a href=\"#t4\">References as Function Return Value<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What are References?<\/strong><\/h4>\n<p>A reference variable or just reference, is a variable that refers to an existing variable. Once you create a reference, then you can refer to to variable using that reference.<\/p>\n<p>Is this not the same as pointers? Well, no. Here are the differences<\/p>\n<p><strong>References vs Pointers<\/strong><\/p>\n<table>\n<tbody>\n<tr style=\"background-color: #f7f6f3; font-weight: bold;\">\n<td>References<\/td>\n<td>Pointers<\/td>\n<\/tr>\n<tr>\n<td>You cannot have NULL references<\/td>\n<td>You can have NULL pointers<\/td>\n<\/tr>\n<tr>\n<td>Once initialized, a reference cannot be changed to reference another object<\/td>\n<td>Pointers can be modified to point to another object at any time<\/td>\n<\/tr>\n<tr>\n<td>A reference must be initialized at creation time<\/td>\n<td>Pointers can be initialized at any time<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Creating References in C++<\/strong><\/h4>\n<p>A reference can be thought of an an alias of a variable name. So in the code below, we have a variable x. So we create a reference variable r to refer to x.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ declare a variables, x<\/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;\">34<\/span>;\r\n\r\n<span style=\"color: #888888;\">\/\/ create a reference rx<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">&amp;<\/span> rx <span style=\"color: #333333;\">=<\/span> x \r\n<\/pre>\n<p>The program below shows the use of references.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t   <span style=\"color: #888888;\">\/\/ declare two normal variables<\/span>\r\n\t   <span style=\"color: #333399; font-weight: bold;\">int<\/span>    a;\r\n\t   <span style=\"color: #333399; font-weight: bold;\">double<\/span> b;\r\n\t \r\n\t   <span style=\"color: #888888;\">\/\/ declare two reference variables<\/span>\r\n\t   <span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">&amp;<\/span>    ra <span style=\"color: #333333;\">=<\/span> a;\r\n\t   <span style=\"color: #333399; font-weight: bold;\">double<\/span><span style=\"color: #333333;\">&amp;<\/span> rb <span style=\"color: #333333;\">=<\/span> b;\r\n\t   \r\n\t   a <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span>;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Value of a : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> a <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Value of ra reference : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> ra  <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t \r\n\t   b <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">70.25<\/span>;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"b has a value of : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> b <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"The reference has a value of : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> rb  <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   \r\n\t   <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 this program is:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Value of a : 100\r\nValue of ra reference : 100\r\nb has a value of : 70.25\r\nThe reference has a value of : 70.25\r\n<\/pre>\n<p>So you can see that references, a like another name for the variable is references.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Passing Parameter by Reference<\/strong><\/h4>\n<p>You may wonder. Why do we need references? Well in many cases, passing parameter by reference would lead to optimized memory utilization. Passing parameter by reference, means that the formal parameters are reference variables. So they are references to the actual parameters. Example is given the the following code.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t   <span style=\"color: #888888;\">\/\/ declare two normal variables<\/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;\">10<\/span>;\r\n\t   <span style=\"color: #333399; font-weight: bold;\">int<\/span> y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>;\r\n\t   \r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Value of x and y before swap\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"x before swap : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"y before swap : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> y  <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   \r\n\t   <span style=\"color: #888888;\">\/\/ call swap() to swap the two numbers<\/span>\r\n\t   swap(x, y);\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Value of x and y after swap\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"x after swap : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"y after swap : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> y  <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n\t   <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 program would give the following output<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Value of x and y before swap\r\nx before swap : 10\r\ny before swap : 20\r\nValue of x and y after swap\r\nx after swap : 20\r\ny after swap : 10\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Reference as Function Return Value<\/strong><\/h4>\n<p>A function have be made to return a reference. When a function returns a reference, then it kind of returns a pointer to its return value<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> scores[] <span style=\"color: #333333;\">=<\/span> { <span style=\"color: #6600ee; font-weight: bold;\">67.5<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">98.8<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">75.7<\/span>};\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span><span style=\"color: #333333;\">&amp;<\/span> setValues( <span style=\"color: #333399; font-weight: bold;\">int<\/span> i ) {\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> scores[i];   <span style=\"color: #888888;\">\/\/ return a reference to the ith element<\/span>\r\n}\r\n\r\n<span style=\"color: #888888;\">\/\/ main function<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> main () {\r\n\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Scores before change\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   <span style=\"color: #008800; font-weight: bold;\">for<\/span> ( <span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>; i <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>; i<span style=\"color: #333333;\">++<\/span> ) {\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"scores[\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> i <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"] = \"<\/span>;\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> scores[i] <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   }\r\n\r\n   setValues(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>) <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">100.0<\/span>; <span style=\"color: #888888;\">\/\/ change 1st element<\/span>\r\n   setValues(<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>) <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">100.0<\/span>;  <span style=\"color: #888888;\">\/\/ change 3rd element<\/span>\r\n\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Scores after change\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   <span style=\"color: #008800; font-weight: bold;\">for<\/span> ( <span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>; i <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>; i<span style=\"color: #333333;\">++<\/span> ) {\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"scores[\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> i <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"] = \"<\/span>;\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> scores[i] <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 program above would produce the following output.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Scores before change\r\nscores[0] = 67.5\r\nscores[1] = 98.8\r\nscores[2] = 75.7\r\nScores after change\r\nscores[0] = 100\r\nscores[1] = 98.8\r\nscores[2] = 100\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The concepts of references\u00a0 is one of the key concepts in programming. This is regardless of the programming language. So ensure you understand it since &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":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/213","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=213"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/213\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}