{"id":84495,"date":"2021-09-03T09:00:04","date_gmt":"2021-09-03T03:30:04","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84495"},"modified":"2021-09-03T09:00:04","modified_gmt":"2021-09-03T03:30:04","slug":"cpp-pointers","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/","title":{"rendered":"C++ Pointers"},"content":{"rendered":"<p>The C++ programming language offers various useful and exciting features and functionalities to programmers. C++ also supports object-oriented programming which makes life easier. C++ delivers many benefits to its users. Pointer is one of the most useful and important topics of any programming language. Let us learn more about pointers in C++.<\/p>\n<h3>What are Pointers in C++?<\/h3>\n<p>A pointer is used to refer to a variable that holds the address of another variable. These are symbolic representations of addresses. Always remember that an integer type pointer will only hold the address of an integer type variable. And same goes for the character.<\/p>\n<p>Pointers enable programs to simulate call-by-reference. Pointers are also used to create and manipulate dynamic data structures.<\/p>\n<h3>Addresses in C++<\/h3>\n<p>Suppose, you have a variable named data in the program code then &amp;data will give you the address in the memory.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main()\n{\n  int data = 10;\n  cout &lt;&lt; \"Address is: \"&lt;&lt; &amp;data;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Address is: 0x7fff8f2f3cd4<\/div>\n<p>In the above example, 0x means the address in hexadecimal format.<\/p>\n<h3>C++ Pointer Declaration<\/h3>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">datatype *name_of_the_variable;\n<\/pre>\n<ul>\n<li>datatype:- It is the base type of the pointer. And it also must be valid C++ data.<\/li>\n<li>And at last, you have to give the name of the variable.<\/li>\n<li>For pointer declaration, we used the asterisk.<\/li>\n<\/ul>\n<p><strong>Example of Valid pointer declarations:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int    *a;    \ndouble *b;   \nfloat  *f;\n<\/pre>\n<h3>Assigning addresses to pointers<\/h3>\n<p>You can assign addresses to pointers.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int* ptr, data;\ndata = 5;\n\nptr = &amp;data;<\/pre>\n<p>In the above code, we assigned the variable data with value 5. And the address of the data variable is assigned to the ptr pointer.<\/p>\n<h3>Getting the value from the address<\/h3>\n<p>You can also get the value from the address using pointers. You will have to use the * operator to do so.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int* ptr, data;\ndata = 10;\nptr = &amp;data;\n\ncout &lt;&lt; *ptr;<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<p>In the above example, the address of data is assigned to the pointer ptr. And we use *ptr to get the value that is stored in that address.<\/p>\n<p><strong>Example:- C++ pointers<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main() {\n    int data = 10;\n\n    \/\/ declare pointer variable\n    int* ptr;\n\n    ptr = &amp;data;\n    cout &lt;&lt; \"data: \" &lt;&lt; data &lt;&lt; endl;\n\n    cout &lt;&lt; \"Address of data: \" &lt;&lt; &amp;data&lt;&lt; endl;\n    cout &lt;&lt; \"ptr: \" &lt;&lt; ptr &lt;&lt; endl;\n\n    cout &lt;&lt; \"Value stored in the address pointed to by pointVar: \" &lt;&lt; *ptr &lt;&lt; endl;\n    \n    return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">data: 10<br \/>\nAddress of data: 0x7ffdb1a8a09c<br \/>\nptr: 0x7ffdb1a8a09c<br \/>\nValue stored in the address pointed to by pointVar: 10<\/div>\n<h3>Change the value pointed by pointers<\/h3>\n<p>You can also change the value that is pointed by pointers.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main() {\n  int data = 10;\n  int* ptr;\n  ptr = &amp;data;\n  cout &lt;&lt; \"data: \" &lt;&lt; data &lt;&lt; endl;\n  cout &lt;&lt; \"*ptr: \" &lt;&lt; *ptr &lt;&lt; endl;\n\n  data = 15; \/\/changing the value of data to 15\n  cout &lt;&lt; \"Now data: \" &lt;&lt; data &lt;&lt; endl;\n  cout &lt;&lt; \"*ptr: \" &lt;&lt; *ptr &lt;&lt; endl;\n  *ptr = 17; \/\/  changing the value of *ptr to 17\n  cout &lt;&lt; \"data: \" &lt;&lt; data &lt;&lt; endl;\n  cout &lt;&lt; \"Now *ptr: \" &lt;&lt; *ptr &lt;&lt; endl;\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">data: 10<br \/>\n*ptr: 10<br \/>\nNow data: 15<br \/>\n*ptr: 15<br \/>\ndata: 17<br \/>\nNow *ptr: 17<\/div>\n<h4>Reference and Dereference Operator<\/h4>\n<ul>\n<li>The reference operator is denoted by \u2018&amp;\u2019. It is used to return the address of the variable.<\/li>\n<li>The dereference operator is denoted by \u2018*\u2019. It is used to get the value that is already stored in a memory address.<\/li>\n<li>The reference operator will return in hexadecimal format and the other will return in decimal format because representation of memory is always in hexadecimal format.<\/li>\n<\/ul>\n<h4>Pointers and Arrays in C++<\/h4>\n<p>You won\u2019t have to use an ampersand(&amp;) to assign the address of an array to a pointer. You can also convert an array to a pointer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int a[20];\nint *ptr;\n<\/pre>\n<p>Below is the valid operation:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ptr=a;<\/pre>\n<p>In the above example, the array a and the pointer ptr will be equivalent.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main() {\n    int *ptr;\n    int a[] = {5,6,9,3};\n    ptr = a;\n    for (int i = 0; i &lt; 4; i++) {\n   \t cout &lt;&lt; *ptr &lt;&lt; endl;\n   \t ptr++;\n    }\n    return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">5<br \/>\n6<br \/>\n9<br \/>\n3<\/div>\n<h4>Null Pointers:-<\/h4>\n<p>A pointer that points to nowhere then it is known as null pointers. There are two methods to assign a null pointer:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int *point1 = 0;\nint *point2 = NULL;\n<\/pre>\n<h4>Invalid Pointers in C++:-<\/h4>\n<p>A pointer is known as an invalid pointer when it does not point to valid elements. You can also state that Uninitialized pointers are also known as invalid pointers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int *a;\nint ar[10];\nint *point = ar+20;\n<\/pre>\n<p>In the above example,<strong> *a<\/strong> pointer is uninitialized so it is an invalid pointer. And the pointer variable <strong>point<\/strong> is exceeding the limit of array <strong>ar.<\/strong> So, it is also an invalid pointer.<\/p>\n<h4>Benefits of using pointers in C++:-<\/h4>\n<ul>\n<li>Pointers help you to reduce the length and the execution of the program.<\/li>\n<li>Using a pointer, you can perform dynamic memory allocation easily.<\/li>\n<li>With pointers, it is easy to handle and implement arrays and structures.<\/li>\n<\/ul>\n<h4>Usage of Pointers:-<\/h4>\n<p>There are many uses of pointers in C++ programming.<\/p>\n<p><strong>1. Dynamic Memory Allocation:-<\/strong> With the help of pointers, we can easily allocate memory dynamically using calloc() and malloc() functions in C++.<\/p>\n<p><strong>2. Arrays, Functions and Structures:-<\/strong> With pointers, it is easy to handle and implement arrays and structures. It helps in reducing code complexity and the execution time.<\/p>\n<h3>Complex Pointers in C++:-<\/h3>\n<p>You must know that there are some complex pointers available in C++. Below is a table of the precedence and associativity of the operators which are used regarding pointers.<\/p>\n<table style=\"height: 234px\" width=\"609\">\n<tbody>\n<tr>\n<td><strong>Operator<\/strong><\/td>\n<td><strong>Precedence<\/strong><\/td>\n<td><strong>Associativity<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(), []<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">Left to Right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">*, identifier\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">Right to Left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Data Type<\/span><\/td>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">_<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li><strong>():-<\/strong> Used to declare and define a function.<\/li>\n<li><strong>[]:-<\/strong> Used to represent an array.<\/li>\n<li><strong>*:-<\/strong> A pointer operator.<\/li>\n<li><strong>Identifier:-<\/strong> Name of the pointer.<\/li>\n<li><strong>Data Type:-<\/strong> It is the type of the variable to which the pointer points to.<\/li>\n<\/ul>\n<h4>Read the pointer:-<\/h4>\n<p>Before starting to read a pointer, you have to check that if <strong>() and []<\/strong> have equal precedence and also look for the associativity.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int (*point)[2]<\/pre>\n<p>In the above example, you will notice that associativity is left to right. In that case, the priority goes to (). Inside the bracket, you will notice that<strong> *(pointer operator)<\/strong> and <strong>point(Identifier)<\/strong> have the same precedence. So, the associativity will go right to left. First priority goes to the <strong>point<\/strong> and the second priority goes to *. At last, you can say that point is a pointer to an array of integers of size 2.<\/p>\n<h3>Pointers of Variables<\/h3>\n<p>Using C++, you can directly manipulate data from the computer\u2019s memory. Pointer variables point to a specific address in the computer\u2019s memory.<\/p>\n<p><strong>Declaration of Pointers of Variables<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int *p;\nor,\nint* p;\n<\/pre>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main() {\n    int *ptr, a = 20;\n    ptr = &amp;a;\n    cout &lt;&lt; \"a: \" &lt;&lt; *ptr;\n    return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">a: 20<\/div>\n<h3>Applications of pointers in C++<\/h3>\n<p>In C++, functions can only return one value. Function arguments are passed by the value. And if you do any modifications on the variables then it won\u2019t change the value of the actual variables which are passed.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n\nusing namespace std;\nvoid func(int*, int*);\nint main() {\n    int a = 6, b = 7;\n    cout &lt;&lt; \"Before:\" &lt;&lt; endl;\n    cout &lt;&lt; \"a: \" &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; \"b: \" &lt;&lt; b &lt;&lt; endl;\n\n    func(&amp;a, &amp;b);\n\n    cout &lt;&lt; \"\\nAfter:\" &lt;&lt; endl;\n    cout &lt;&lt; \"a: \" &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; \"b: \" &lt;&lt; b &lt;&lt; endl;\n    return 0;\n}\n\nvoid func(int* x, int* y) {\n    *x = 5;\n    *y = 4;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Before:<br \/>\na: 6<br \/>\nb: 7After:<br \/>\na: 5<br \/>\nb: 4<\/div>\n<p>You can use pointers as the function arguments to pass the variable\u2019s actual address in the function. The modifications which you have done on the variables will affect the outer function.<br \/>\nIn the above example, we created a function named func and it has the address of the variables a and b.<\/p>\n<h3>Pointer Arithmetic:-<\/h3>\n<p>In a pointer, you can use four arithmetic operators such as ++, &#8211;, +, and &#8211; on pointers. With the help of this, you can perform certain arithmetic operations on pointers.<\/p>\n<h4>Incrementing a pointer:-<\/h4>\n<p>In C++, you can also increment a pointer. You can use an array that will help you increment the pointer easily.<br \/>\n<strong>Example:- incrementing a pointer<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main () {\n   int value_array[2] = {41, 1};\n   int *point;\n   point = value_array;\n   cout&lt;&lt;\"TechVidvan Tutorial: Incrementing a pointer!\"&lt;&lt;endl&lt;&lt;endl;\n   for (int a = 0; a &lt; 2; a++) {\n   \tcout&lt;&lt;\"Value of value_array: \"&lt;&lt;*point&lt;&lt;endl;\n   \tcout&lt;&lt;\"Address of value_array: \"&lt;&lt;point&lt;&lt;endl;\n   \tpoint++;\/\/ incrementing the pointer variable!\n   }\n   return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Incrementing a pointer!<\/p>\n<p>Value of value_array: 41<br \/>\nAddress of value_array: 0x7ffea960cbf0<br \/>\nValue of value_array: 1<br \/>\nAddress of value_array: 0x7ffea960cbf4<\/p>\n<\/div>\n<h4>Decrementing a pointer in C++:-<\/h4>\n<p>The C++ programming language also has the functionality to decrement a pointer.<\/p>\n<p><strong>Example:- Decrementing a pointer<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main () {\n   int value_array[] = {141, 102};\n   int *point;\n   int max=2;\n   point = &amp;value_array[max-1];\n   cout&lt;&lt;\"TechVidvan Tutorial: Decrementing a pointer!\"&lt;&lt;endl&lt;&lt;endl;\n   for (int a = max; a &gt; 0; a--) {\n   \tcout&lt;&lt;\"Value of value_array: \"&lt;&lt;*point&lt;&lt;endl;\n   \tcout&lt;&lt;\"Address of value_array: \"&lt;&lt;point&lt;&lt;endl;\n  point--; \/\/ decrementing the pointer variable!\n   }\n   return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Decrementing a pointer!<br \/>\nValue of value_array: 102<br \/>\nAddress of value_array: 0x7ffd201d5c24<br \/>\nValue of value_array: 141<br \/>\nAddress of value_array: 0x7ffd201d5c20<\/div>\n<h3>Pointer Comparison:-<\/h3>\n<p>You can also compare pointers by using relational operators like ==, &lt;, and &gt;.<br \/>\n<strong>Example:- Pointer Comparison<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main () {\n   int value_array[] = {141, 1032};\n   int *point;\n   int max=2;\n   int a=0;\n   point = value_array;\n   cout&lt;&lt;\"TechVidvan Tutorial: Pointer Comparison!\"&lt;&lt;endl;\n   while(point &lt;= &amp;value_array[max-1]) {\n  cout&lt;&lt;\"Value of value_array: \"&lt;&lt;*point&lt;&lt;endl;\n  cout&lt;&lt;\"Address of value_array: \"&lt;&lt;point&lt;&lt;endl;\n  point++;\n  a++;\n   }\n   return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Pointer Comparison!<br \/>\nValue of value_array: 141<br \/>\nAddress of value_array: 0x7ffe95588b90<br \/>\nValue of value_array: 1032<br \/>\nAddress of value_array: 0x7ffe95588b94<\/div>\n<p>In the above example, we are incrementing the pointer variable until the address which the pointer points to is less than or equal to the address of the last element of the array.<\/p>\n<h4>Pointer to Pointer:-<\/h4>\n<p>Pointer to pointer is known as a chain of pointers. It means that the first pointer contains the address of the second pointer and it points to the location of actual value.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int **point;\n<\/pre>\n<p>In the above example, we declared a pointer to pointer of type integer.<br \/>\n<strong>Example:- Pointer to pointer<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main () {\n   int  val;\n   int  *pt;\n   int  **point;\n   val = 55;\n   pt = &amp;val;\n   point = &amp;pt;\n   cout&lt;&lt;\"Value=&gt; val: \"&lt;&lt;val&lt;&lt;endl;\n   cout&lt;&lt;\"Value=&gt; *pt: \"&lt;&lt;*pt&lt;&lt;endl;\n   cout&lt;&lt;\"Value=&gt; **pptr: \"&lt;&lt;**point&lt;&lt;endl;\nreturn 0;\n}   \n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Value=&gt; val: 55<br \/>\nValue=&gt; *pt: 55<br \/>\nValue=&gt; **pptr: 55<\/div>\n<p>In C++, there are 3 ways to pass arguments to a function:-<\/p>\n<ul>\n<li>Call by value<\/li>\n<li>Call by reference<\/li>\n<li>Call by reference with reference argument<\/li>\n<\/ul>\n<p><strong>Example:- Call by methods in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nint func1(int num)\n{\n    cout &lt;&lt; \"func1()=&gt; address of num1: \" &lt;&lt; &amp;num &lt;&lt; endl;\n    num += num;\n    return num;\n}\nvoid func2(int *num)\n{\n    cout &lt;&lt; \"func2()=&gt; num2 address: \" &lt;&lt; num &lt;&lt; \"\\n\";\n    *num += *num;\n}\nvoid func3(int &amp;num)\n{\n    cout &lt;&lt; \"func3()=&gt; num3 address: \" &lt;&lt; &amp;num &lt;&lt; \"\\n\";\n    num += num;\n}\nvoid Tech()\n{\n    \/\/Call-by-Value\n    int num1=8;\n    cout &lt;&lt; \"main()=&gt; num1 address: \" &lt;&lt; &amp;num1 &lt;&lt; \"\\n\";\n    cout &lt;&lt; \"num1: \" &lt;&lt; func1(num1) &lt;&lt; \"\\n\";\n    cout &lt;&lt; \"No change in num1: \" &lt;&lt; num1 &lt;&lt; \"\\n\";\n    \n    \/\/Call-by-Reference with Pointer Arguments\n    int num2=8;\n    cout &lt;&lt; \"main()=&gt; num2 address: \" &lt;&lt; &amp;num2 &lt;&lt; \"\\n\";\n    func2(&amp;num2);\n    cout &lt;&lt; \"num2: \" &lt;&lt; num2 &lt;&lt; \"\\n\";\n    cout &lt;&lt; \"Change in num2: \" &lt;&lt; num2 &lt;&lt; \"\\n\";\n    \n    \/\/Call-by-Reference with Reference Arguments\n    int num3=8;\n    cout &lt;&lt; \"main()=&gt; num3 address: \" &lt;&lt; &amp;num3 &lt;&lt; \"\\n\";\n    func3(num3);\n    cout &lt;&lt; \"num3: \" &lt;&lt; num3 &lt;&lt; \"\\n\";\n    cout &lt;&lt; \"Change in num3: \" &lt;&lt; num3 &lt;&lt; \"\\n\";\n    \n}\nint main()\n{\n    Tech();\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">main()=&gt; num1 address: 0x7ffe5421b7fc<br \/>\nnum1: func1()=&gt; address of num1: 0x7ffe5421b7dc<br \/>\n16<br \/>\nNo change in num1: 8<br \/>\nmain()=&gt; num2 address: 0x7ffe5421b800<br \/>\nfunc2()=&gt; num2 address: 0x7ffe5421b800<br \/>\nnum2: 16<br \/>\nChange in num2: 16<br \/>\nmain()=&gt; num3 address: 0x7ffe5421b804<br \/>\nfunc3()=&gt; num3 address: 0x7ffe5421b804<br \/>\nnum3: 16<br \/>\nChange in num3: 16<\/div>\n<p>The arguments are passed by value in C++. You can make use of pass by reference to avoid the overhead of cloning. Changes are made into the clone that is made by the called function.<\/p>\n<h4>Return pointers from function:-<\/h4>\n<p>In C++, you have seen that you can return an array from a function. In a similar way, you can also return a pointer from a function.<\/p>\n<p><strong>NOTE:-<\/strong> If you try to return the address of a local variable outside the function then the local variables of the function will go out of scope. And your program will never get executed. So to avoid this type of error, you have to define the local variable as a static variable.<\/p>\n<p><strong>Example:- Return a pointer from a function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint* printToscreen()\n{\n  static int val = 125; \/\/ defining the local variable as static to avoid execution error!\n  return (&amp;val); \/\/ function returning pointer!\n}\nint main()\n{\n  int* point;\n  point = printToscreen();\n  cout&lt;&lt;\"TechVidvan Tutorial: Returning pointer from a function!\"&lt;&lt;endl&lt;&lt;endl;\n  cout&lt;&lt;\"Address is: \"&lt;&lt;point&lt;&lt;endl;\n  cout&lt;&lt;\"Value is: \"&lt;&lt;*point&lt;&lt;endl;\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Returning pointer from a function!<\/p>\n<p>Address is: 0x55d2974f6010<br \/>\nValue is: 125<\/p>\n<\/div>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed the pointers and addresses in C++. We also discussed how you can perform different operations on a pointer in C++. We discussed the benefits of using pointers in C++. Then we discussed how to return pointers from a function. We talked over how you can assign a pointer to a pointer in C++. We discussed the applications of pointers in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various useful and exciting features and functionalities to programmers. C++ also supports object-oriented programming which makes life easier. C++ delivers many benefits to its users. Pointer is one of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84561,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4172,4173,4174,4175,4176,4177,4178],"class_list":["post-84495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-addresses-in-c","tag-benefits-of-pointers","tag-c-pointers","tag-invalid-pointers-in-c","tag-null-pointers-in-c","tag-pointer-declaration-in-c","tag-usage-of-pointers-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Pointers - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about pointers &amp; addresses in C++. See benefits &amp; different operations on a pointer in C++. Learn how to return pointers from function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Pointers - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about pointers &amp; addresses in C++. See benefits &amp; different operations on a pointer in C++. Learn how to return pointers from function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-03T03:30:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Pointers - TechVidvan","description":"Learn about pointers & addresses in C++. See benefits & different operations on a pointer in C++. Learn how to return pointers from function.","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:\/\/techvidvan.com\/tutorials\/cpp-pointers\/","og_locale":"en_US","og_type":"article","og_title":"C++ Pointers - TechVidvan","og_description":"Learn about pointers & addresses in C++. See benefits & different operations on a pointer in C++. Learn how to return pointers from function.","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-03T03:30:04+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Pointers","datePublished":"2021-09-03T03:30:04+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/"},"wordCount":1613,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg","keywords":["Addresses in C++","benefits of pointers","C++ Pointers","invalid pointers in C++","Null pointers in C++","Pointer Declaration in C++","usage of pointers in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/","name":"C++ Pointers - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg","datePublished":"2021-09-03T03:30:04+00:00","description":"Learn about pointers & addresses in C++. See benefits & different operations on a pointer in C++. Learn how to return pointers from function.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Pointers.jpg","width":1200,"height":628,"caption":"C++ Pointers"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-pointers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Pointers"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84495"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84495\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84561"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}