{"id":2711,"date":"2024-05-10T11:21:33","date_gmt":"2024-05-10T11:21:33","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2711"},"modified":"2024-05-10T11:21:33","modified_gmt":"2024-05-10T11:21:33","slug":"c-pointer-to-pointer-double-pointer","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-pointer-to-pointer-double-pointer\/","title":{"rendered":"C \u2013 Pointer To Pointer (Double 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=\"#c-pointer-to-pointer-double-pointer\">C \u2013 Pointer To Pointer (Double Pointer)<\/a><\/li><li ><a href=\"#declaration-of-pointer-to-a-pointer-in-c\">Declaration of Pointer to a Pointer in C<\/a><\/li><li ><a href=\"#example-of-double-pointer-in-c\">Example of Double Pointer in C<\/a><\/li><li ><a href=\"#how-double-pointer-works\">How Double Pointer Works?<\/a><\/li><li ><a href=\"#size-of-pointer-to-pointer-in-c\">Size of Pointer to Pointer in C<\/a><\/li><li ><a href=\"#application-of-double-pointers-in-c\">Application of Double Pointers in C<\/a><\/li><li ><a href=\"#multilevel-pointers-in-c\">Multilevel Pointers in C<\/a><ul><li ><a href=\"#syntax-of-triple-pointer\">Syntax of Triple Pointer<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-c-pointer-to-pointer-double-pointer\">FAQ- C \u2013 Pointer To Pointer (Double Pointer)<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-pointer-to-pointer-double-pointer\">C \u2013 Pointer To Pointer (Double Pointer)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A pointer-to-pointer (double pointer) is used to store the address of another pointer. The first pointer stores the address of a variable, and the second pointer stores the address of the first pointer. This makes them useful for changing the values of normal pointers or creating variable-sized 2-D arrays. Importantly, a double pointer occupies the same amount of memory as a regular pointer on the stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declaration-of-pointer-to-a-pointer-in-c\">Declaration of Pointer to a Pointer in C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We know that declaring a pointer will be similar to declaring a double pointer in C. The only difference, we can notice is the adding of * just before the pointer name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data_type_of_pointer **name_of_variable = &amp; normal_pointer_variable;\nint val = 5;\nint *ptr = &amp;val;    \/\/ storing address of val to pointer ptr.\nint **d_ptr = &amp;ptr; \/\/ pointer to a pointer declared\n                    \/\/ which is pointing to an integer. <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, this diagram illustrates the first pointer ptr 1 will store the address of the variable and the second pointer ptr 2 can store the address of the first pointer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-double-pointer-in-c\">Example of Double Pointer in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate pointer to pointer\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int var = 789;\n \n    \/\/ pointer for var\n    int* ptr2;\n \n    \/\/ double pointer for ptr2\n    int** ptr1;\n \n    \/\/ storing address of var in ptr2\n    ptr2 = &amp;var;\n \n    \/\/ Storing address of ptr2 in ptr1\n    ptr1 = &amp;ptr2;\n \n    \/\/ Displaying value of var using\n    \/\/ both single and double pointers\n    printf(\"Value of var = %d\\n\", var);\n    printf(\"Value of var using single pointer = %d\\n\", *ptr2);\n    printf(\"Value of var using double pointer = %d\\n\", **ptr1);\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>Value of var = 789\nValue of var using single pointer = 789\nValue of var using double pointer = 789<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-double-pointer-works\">How Double Pointer Works?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Int var =10;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int*ptr =&amp;var;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">*ptr=20;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Int**ptr=&amp;ptr;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">**ptr=30;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declare a double-pointer with the appropriate syntax.<\/li>\n\n\n\n<li>Store the address of another pointer as the value of the double-pointer.<\/li>\n\n\n\n<li>To manipulate or dereference to any of its levels, use the asterisk (<code>*<\/code>) operator the number of times needed to reach the desired level.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"size-of-pointer-to-pointer-in-c\">Size of Pointer to Pointer in C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A double-pointer acts similarly to a normal pointer in C. Also, the size of the pointer is equal to a double pointer.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 1: C Program to find the size of a pointer to a pointer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to find the size of pointer to pointer\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ defining single and double pointers\n    int a = 5;\n    int* ptr = &amp;a;\n    int** d_ptr = &amp;ptr;\n \n    \/\/ size of single pointer\n    printf(\" Size of normal Pointer: %d \\n\", sizeof(ptr));\n \n    \/\/ size of double pointer\n    printf(\" Size of Double Pointer: %d \\n\", sizeof(d_ptr));\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> Size of normal Pointer: 8 \n Size of Double Pointer: 8 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The size of a pointer in C is not fixed and can vary based on factors like CPU architecture and the operating system. Typically, for a 64-bit operating system, pointer sizes are 8 bytes, while for a 32-bit operating system, they are 4 bytes. This size variation is important to consider when working with pointers, especially when dealing with memory allocation and ensuring your code is compatible across different platforms and systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"application-of-double-pointers-in-c\">Application of Double Pointers in C<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Dynamic Memory Allocation: Double-pointers are used to allocate and manage memory for multidimensional arrays, enabling flexible data structures.<\/li>\n\n\n\n<li>Multilevel Data: They are used to store and manage complex hierarchical data structures, like text documents with paragraphs, sentences, and words.<\/li>\n\n\n\n<li>Data Structures: In data structures, double-pointers allow direct manipulation of node addresses without copying data, improving efficiency.<\/li>\n\n\n\n<li>Function Arguments: Double-pointers can be used as function arguments to modify the addresses stored in local pointers, facilitating efficient data manipulation.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multilevel-pointers-in-c\">Multilevel Pointers in C<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In C, we have multilevel pointers beyond double pointers.<\/li>\n\n\n\n<li>For changing the value of a double pointer, we can use a triple pointer, such as <code>int ***t_ptr<\/code>.<\/li>\n\n\n\n<li>Syntax: <code>pointer_type ***pointer_name;<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-of-triple-pointer\">Syntax of Triple Pointer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pointer_type *** pointer_name;  \n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This concept extends further, allowing us to use &#8220;level &#8211; x + 1&#8221; pointers to change the value of a &#8220;level &#8211; x&#8221; variable, and this can be carried to higher levels as needed.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-pointer-to-pointer-double-pointer\">FAQ- C \u2013 Pointer To Pointer (Double 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-1697451639177\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to return a double pointer array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Declaring a 2D array like <code>int input[][c_in]<\/code> doesn&#8217;t make the computer interpret these as pointers. <code>input[x][y]<\/code> represents integers directly, not memory addresses. If you&#8217;re getting addresses when printing, check your code for any issues. Use <code>printf(\"%d\", input[x][y]);<\/code> to print integer values.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697451674862\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the advantage of a double-pointer?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Double pointers in C are powerful tools for efficiently managing data in memory. They allow you to access and manipulate memory addresses, making them versatile for tasks like data reorganization. A pointer, as the name suggests, points to a specific location in memory, enabling you to work with data effectively.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697451686715\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the two types of pointers in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Those are four types of pointers. Those are :<br \/>Null Pointer.<br \/>Void Pointer.<br \/>Wild Pointer.<br \/>Dangling Pointer.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C \u2013 Pointer To Pointer (Double Pointer) A pointer-to-pointer (double pointer) is used to store the address of another pointer. The first pointer stores the address of a variable, and the second pointer stores the address of the first pointer. This makes them useful for changing the values of normal pointers or creating variable-sized 2-D &#8230; <a title=\"C \u2013 Pointer To Pointer (Double Pointer)\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-pointer-to-pointer-double-pointer\/\" aria-label=\"More on C \u2013 Pointer To Pointer (Double Pointer)\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5373,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[464],"class_list":["post-2711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-pointer-to-pointer-double-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\/2711","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=2711"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2711\/revisions"}],"predecessor-version":[{"id":10726,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2711\/revisions\/10726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5373"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}