{"id":2716,"date":"2024-05-10T11:21:42","date_gmt":"2024-05-10T11:21:42","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2716"},"modified":"2024-05-10T11:21:42","modified_gmt":"2024-05-10T11:21:42","slug":"pointer-to-an-array-array-pointer","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/pointer-to-an-array-array-pointer\/","title":{"rendered":"Pointer to an Array | Array Pointer"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#pointer-to-an-array-array-pointer\">Pointer to an Array | Array Pointer<\/a><\/li><li ><a href=\"#program-1-difference-between-pointer-to-an-integer-and-pointer-to-array-of-integers\">Program 1 &#8211; Difference Between Pointer to an Integer and Pointer to Array of integers<\/a><\/li><li ><a href=\"#program-2-to-illustrate-the-size-of-an-array\">Program 2 &#8211; To illustrate the size of an Array<\/a><\/li><li ><a href=\"#faq-pointer-to-an-array-array-pointer\">FAQ- Pointer to an Array | Array Pointer<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pointer-to-an-array-array-pointer\">Pointer to an Array | Array Pointer<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\nint arr&#91;5] = { 1, 2, 3, 4, 5 };\nint *ptr = arr;\n \ncout &lt;&lt;\"\\n\"&lt;&lt; ptr;\nreturn 0;\n}\n <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This type of pointer is typically referred to as a &#8220;pointer to an array.&#8221; It allows you to work with entire arrays, which can be particularly useful for managing multidimensional data structures and passing arrays as function arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>data_type (*var_name)&#91;size_of_array];\n<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int (*ptr)&#91;10];\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To declare a pointer that can point to an array of 10 integers, you should use parentheses to ensure the proper order of operations. In this case, the type of <code>ptr<\/code> is indeed a &#8220;pointer to an array of 10 integers,&#8221; and it&#8217;s essential to define it correctly for proper memory manipulation and data access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-1-difference-between-pointer-to-an-integer-and-pointer-to-array-of-integers\">Program 1 &#8211; Difference Between Pointer to an Integer and Pointer to Array of integers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Whereas, the pointer points to the Oth element of the array and the pointer that pointer that points to the whole array are both different. The program below illustrates that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ program to understand difference between \n\/\/ pointer to an integer and pointer to an\n\/\/ array of integers. \n#include &lt;iostream&gt;\nusing namespace std;\nint main()\n{\n    \/\/ Pointer to an integer\n    int *p; \n     \n    \/\/ Pointer to an array of 5 integers\n    int (*ptr)&#91;5]; \n    int arr&#91;5];\n     \n    \/\/ Points to 0th element of the arr.\n    p = arr;\n     \n    \/\/ Points to the whole array arr.\n    ptr = &amp;arr; \n     \n    cout &lt;&lt; \"p =\" &lt;&lt; p &lt;&lt;\", ptr = \"&lt;&lt; ptr&lt;&lt; endl;\n    p++; \n    ptr++;\n    cout &lt;&lt; \"p =\" &lt;&lt; p &lt;&lt;\", ptr = \"&lt;&lt; ptr&lt;&lt; endl;\n     \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = 0x7fff4f32fd50, ptr = 0x7fff4f32fd50\np = 0x7fff4f32fd54, ptr = 0x7fff4f32fd64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can analyze that p is the integer and, ptr is the pointer, to an array of 5 integers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-2-to-illustrate-the-size-of-an-array\">Program 2 &#8211; To illustrate the size of an Array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you dereference a pointer to an array in C, you get the base address (i.e., the address of the first element) of the array to which the pointer points. This is because the name of an array in C represents its base address. So, dereferencing a pointer to an array effectively gives you the base address of that array, allowing you to work with the array&#8217;s elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ program to illustrate sizes of\n\/\/ pointer of array\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\n \nint main()\n{\n    int arr&#91;] = { 3, 5, 6, 7, 9 };\n    int *p = arr;\n    int (*ptr)&#91;5] = &amp;arr;\n     \n    cout &lt;&lt; \"p = \"&lt;&lt; p &lt;&lt;\", ptr = \" &lt;&lt; ptr &lt;&lt; endl;\n    cout &lt;&lt; \"*p = \"&lt;&lt; *p &lt;&lt;\", *ptr = \" &lt;&lt; *ptr &lt;&lt; endl;\n     \n    cout &lt;&lt; \"sizeof(p) = \"&lt;&lt; sizeof(p) &lt;&lt;\n            \", sizeof(*p) = \" &lt;&lt; sizeof(*p) &lt;&lt; endl;\n    cout &lt;&lt; \"sizeof(ptr) = \"&lt;&lt; sizeof(ptr) &lt;&lt;\n        \", sizeof(*ptr) = \" &lt;&lt; sizeof(*ptr) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = 0x7ffde1ee5010, ptr = 0x7ffde1ee5010\n*p = 3, *ptr = 0x7ffde1ee5010\nsizeof(p) = 8, sizeof(*p) = 4\nsizeof(ptr) = 8, sizeof(*ptr) = 20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-pointer-to-an-array-array-pointer\">FAQ- Pointer to an Array | Array Pointer<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697457913042\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How do you assign a pointer to an array of pointers?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The statement <code>int *pa = &amp;a[0];<\/code> assigns the memory address of the first element, <code>a[0]<\/code>, in the array <code>a<\/code> to the pointer, which is of type <code>int<\/code>. This is a common way to initialize a pointer to point to the first element of an array. It allows you to work with the array&#8217;s elements using the pointer.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697457921312\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to use pointer to pointer in an array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. #include&lt;stdio.h><br \/>void main ()<br \/>{<br \/>int a = 10;<br \/>int *p;<br \/>int **pp;<br \/>p = &amp;a; \/\/ pointer p is pointing to the address of a.<br \/>pp = &amp;p; \/\/ pointer pp is a double pointer pointing to the address of pointer p<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697457928938\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Is the ptr a pointer to an array?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>ptr<\/code> is a pointer to an entire array. When you increment it by 1, it moves to the next block of 5 elements if it&#8217;s an array of 5 elements.<br \/><code>*ptr<\/code> is a pointer to the first element of the array. When you increment it by 1, it moves to point to the second element within the array. This is because <code>*ptr<\/code> is equivalent to <code>ptr[0]<\/code>, and incrementing it essentially accesses the next element in the array.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Pointer to an Array | Array Pointer This type of pointer is typically referred to as a &#8220;pointer to an array.&#8221; It allows you to work with entire arrays, which can be particularly useful for managing multidimensional data structures and passing arrays as function arguments. Syntax Example To declare a pointer that can point to &#8230; <a title=\"Pointer to an Array | Array Pointer\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/pointer-to-an-array-array-pointer\/\" aria-label=\"More on Pointer to an Array | Array Pointer\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5375,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[465],"class_list":["post-2716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-pointer-to-an-array-array-pointer","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2716"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2716\/revisions"}],"predecessor-version":[{"id":10727,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2716\/revisions\/10727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5375"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}