{"id":497,"date":"2021-01-17T05:24:40","date_gmt":"2021-01-17T05:24:40","guid":{"rendered":"https:\/\/usemynotes.com\/?p=497"},"modified":"2023-02-20T17:20:49","modified_gmt":"2023-02-20T17:20:49","slug":"what-are-pointers-in-c-programming","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/","title":{"rendered":"What are Pointers in C Programming? &#038; How to declare"},"content":{"rendered":"<p>Welcome back guys, in this module, we are going to discuss what are pointers in <a href=\"https:\/\/usemynotes.com\/c-programming\/\">C Programming<\/a>, why we need pointers, what are its advantages and disadvantages, their uses, etc.<\/p>\n<p>So, let\u2019s dive into this module.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#What_are_Pointers_in_C_Programming\" >What are Pointers in C Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#How_to_declare_a_pointer_variable_in_C_Programming\" >How to declare a pointer variable in C Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#How_to_assign_the_addresses_to_the_Pointers_in_C_Programming\" >How to assign the addresses to the Pointers in C Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#What_is_a_NULL_pointer_in_C_Programming\" >What is a NULL pointer in C Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#What_are_the_advantages_of_pointers_in_C_Programming\" >What are the advantages of pointers in C Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/usemynotes.com\/what-are-pointers-in-c-programming\/#What_are_the_disadvantages_of_pointers_in_C_programming\" >What are the disadvantages of pointers in C programming?<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_are_Pointers_in_C_Programming\"><\/span>What are Pointers in C Programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Pointers in C Programming are used to store the address of a particular variable, instead of storing values. It becomes very easy to analyze the program while using pointers. It can easily handle various tasks which can\u2019t be executed using simple programming such as dynamic memory allocation. We can\u2019t perform this without using a pointer.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-498 size-full aligncenter\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming.jpg\" alt=\"What are Pointers in C Programming\" width=\"597\" height=\"603\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming.jpg 597w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming-297x300.jpg 297w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming-150x152.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming-300x303.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/01\/what-are-pointers-in-c-programming-416x420.jpg 416w\" sizes=\"(max-width: 597px) 100vw, 597px\" \/><\/p>\n<p>We know that each and every variable is a memory location and that memory location has its own address defined. So, we can access that address by using an ampersand (&amp;) <a href=\"https:\/\/usemynotes.com\/what-are-operators-in-c\/\">operator<\/a>. This operator denotes the address in the memory.<\/p>\n<p>Let\u2019s understand <strong>Pointers in C Programming<\/strong> by taking a simple example.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\nint main ( ) {\r\n int num1;      \/\/ integer variable declared\r\n char ch&#x5B;5];     \/\/ character variable declared \r\n   \/\/ accessing the address of int variable \r\n   printf(&quot;The Address of integer variable is: %x\\n&quot;, &amp;num1 ); \r\n   \/\/ accessing the address of char variable                  \r\n   printf(&quot;The Address of character variable is: %x\\n&quot;, &amp;ch );         \r\n return 0;\r\n}<\/pre>\n<p>The output of the above <strong>Pointers in C Programming <\/strong>is:<\/p>\n<table>\n<tbody>\n<tr>\n<td>The address of integer variable is: 4e6d292c<br \/>\nThe address of character variable is: 4e6d2933<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the above program, we have simply declared 2 variables of integer and character <a href=\"https:\/\/usemynotes.com\/data-types-in-c-programming\/\">data type<\/a> respectively, and then accessed their addresses by using an ampersand operator and got the resultant output.<\/p>\n<p>In simple terms, we can say that <strong>Pointers in C Programming <\/strong>are variables, whose value signifies the address of another variable, i.e., you can assign the direct address of the memory location to some variable.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_declare_a_pointer_variable_in_C_Programming\"><\/span>How to declare a pointer variable in C Programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The general syntax of declaring the <strong>Pointers in C Programming<\/strong>\u00a0is:<br \/>\n<strong>data_type *var_name ;<\/strong><\/p>\n<p>Here, in this syntax data_type represents the base type of the pointer, it must be a relevant C data type. var_name is the pointer variable name, asterisk representing that the pointer <a href=\"https:\/\/usemynotes.com\/variables-and-constants-in-c-programming\/\">variable<\/a> has been declared.<\/p>\n<p>Some of the valid declarations of the <strong>Pointers in C Programming <\/strong>are:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int *ip ;    \/\/ pointer to an integer declaration\r\ndouble *d ;    \/\/ pointer to a double declaration\r\nfloat *f ;    \/\/ pointer to a float declaration\r\nchar *c ;    \/\/ pointer to a character declaration<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"How_to_assign_the_addresses_to_the_Pointers_in_C_Programming\"><\/span>How to assign the addresses to the Pointers in C Programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us understand this by taking the example of <strong>Pointers in C Programming<\/strong>.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int *p ;\r\nint c = 5 ;\r\np = &amp;c ;<\/pre>\n<p>In the above <strong>Pointers in C Programming<\/strong> example, we have declared a pointer to integer variable p, and a normal variable c, in which we have assigned the value 5, and then assigned the address of the variable c to the pointer p. Let&#8217;s see the program to understand more clearly.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\nint main ( ) { \r\n   int  c = 5;  \/\/ actual variable declaration \r\n   int  *p;   \/\/ pointer to an integer variable declaration\r\n   p = &amp;c;  \/\/ store address of var in pointer variable\r\n\r\n   printf(&quot;Address of the variable is: %x\\n&quot;, &amp;c );\r\n\r\n   \/\/ address stored in pointer variable \r\n   printf(&quot;Address stored in pointer is: %x\\n&quot;, p );\r\n\r\n   \/\/ access the value using the pointer \r\n   printf(&quot;Value stored in pointer is: %d\\n&quot;, *p );\r\nreturn 0;\r\n}<\/pre>\n<p>The output of the above program is:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Address of the variable is: 489371f4<br \/>\nAddress stored in pointer variable is: 489371f4<br \/>\nValue stored in pointer variable is: 5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the above program, we have declared a pointer, assign it to some value, and then printed the address of the variable and the value stored in that pointer variable.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_a_NULL_pointer_in_C_Programming\"><\/span>What is a NULL pointer in C Programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you don\u2019t have any address to assign to a pointer variable, then in that case you can assign a NULL value. It can be done at the time of declaration of the pointer variable. It is a good practice to use a NULL value and the pointer which is assigned a NULL value is called a NULL pointer.<\/p>\n<p>Consider this through the following program in C Programming.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\nint main () {\r\n   int  *p = NULL;   \/\/ pointer variable assigned to NULL\r\n   printf(\u201cValue *p is : %x\\n&quot;, p  );\r\n   return 0;\r\n}<\/pre>\n<p>The <strong>output<\/strong> of the above program is:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Value *p is : 0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the above program, we have assigned a NULL to the pointer variable and then got the value of the pointer to be 0 as output.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_are_the_advantages_of_pointers_in_C_Programming\"><\/span>What are the advantages of pointers in C Programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s see some of the advantages of using pointers in C programming.<\/p>\n<ul>\n<li>It reduces the complexity of the program.<\/li>\n<li>It helps in reducing the storage space of the program.<\/li>\n<li>It reduces the program execution time.<\/li>\n<li>It allows us to resize the memory dynamically.<\/li>\n<li>Through pointers, you have direct access to memory.<\/li>\n<li>It allows us to perform memory allocation and deallocation dynamically.<\/li>\n<li>It allows us to access the address of the particular variable.<\/li>\n<li>We can handle structures and <a href=\"https:\/\/usemynotes.com\/what-are-arrays-in-c-programming\/\">arrays<\/a> in a more efficient way by using pointers.<\/li>\n<li>We can\u2019t able to work in complex data structures such as linked lists, stacks, etc, without using pointers.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"What_are_the_disadvantages_of_pointers_in_C_programming\"><\/span>What are the disadvantages of pointers in C programming?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Well apart from having plenty of advantages, it has some disadvantages or drawbacks also. Let\u2019s see some of them.<\/p>\n<ul>\n<li>It is very slow as compared to normal variables.<\/li>\n<li>It may lead to the corruption of memory if it is updated with wrong or incorrect values.<\/li>\n<li>It may lead to a memory leak because dynamically allocated blocks need to be explicitly freed.<\/li>\n<li>It may cause to segmentation fault if it is not initialized properly.<\/li>\n<li>The program may sometimes crash if sufficient memory is not available during the run time for the pointer storage.<\/li>\n<\/ul>\n<p>I hope, this module helped you all a lot in understanding this wonderful concept of <strong>Pointers in C Programming<\/strong>, and you all must be excited about the upcoming wonderful and exciting topics of this series.<\/p>\n<p>So, until then stay connected, Happy Learning, Keep practising, and Keep coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back guys, in this module, we are going to discuss what are pointers in C Programming, why we need pointers, what are its advantages and disadvantages, their uses, etc. So, let\u2019s dive into this module. What are Pointers in C Programming? Pointers in C Programming are used to store the address of a particular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":{"0":"post-497","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-c-programming"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/498"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}