<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jiayi Su</title>
    <description>The latest articles on DEV Community by Jiayi Su (@jiayisu).</description>
    <link>https://dev.to/jiayisu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1055151%2F40964b93-54ac-4012-a6b2-f3cea64ec8ab.jpeg</url>
      <title>DEV Community: Jiayi Su</title>
      <link>https://dev.to/jiayisu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jiayisu"/>
    <language>en</language>
    <item>
      <title>How to Work with Pointers in C Programming</title>
      <dc:creator>Jiayi Su</dc:creator>
      <pubDate>Thu, 30 Mar 2023 19:42:41 +0000</pubDate>
      <link>https://dev.to/jiayisu/how-to-work-with-pointers-in-c-programming-117b</link>
      <guid>https://dev.to/jiayisu/how-to-work-with-pointers-in-c-programming-117b</guid>
      <description>&lt;p&gt;The true power and potential of dealing directly with memory are unlocked by the pointers feature of the C programming language. With pointers, you can design complicated data structures like arrays, strings, and structures that can optimize memory usage because you have &lt;strong&gt;direct access to the memory addresses&lt;/strong&gt; of your data. &lt;/p&gt;

&lt;p&gt;In this post, we'll go in-depth on pointers in C programming and look at how they function with different data types. You'll be able to create more effective and robust programs that fully utilize the enormous computational power that C offers by grasping the complexities of pointers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic Usage of Pointers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pointers with Integers
&lt;/h3&gt;

&lt;p&gt;A pointer is a variable that stores the memory address of another variable. In other words, a pointer points to a specific memory location in the computer's memory. The kind of data that a pointer variable will point to must be specified when the variable is declared. Use the following syntax, for instance, to declare a pointer that points to an integer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This declares a pointer variable named &lt;code&gt;ptr&lt;/code&gt; that has the ability to point to an integer value.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; operator, which retrieves a variable's memory address, can be used here to fetch the memory address and then assign it to a pointer variable. For instance, you would use the following syntax to provide the pointer variable &lt;code&gt;ptr&lt;/code&gt; the memory address of a variable named &lt;code&gt;a&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The memory address of the variable &lt;code&gt;a&lt;/code&gt; is now stored in &lt;code&gt;ptr&lt;/code&gt;. The value stored at the memory address that the pointer points to can be accessed using the * operator. For instance, you would use the following syntax to display the value of the variable &lt;code&gt;a&lt;/code&gt; using the pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the value 9, which is the value stored in the variable &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pointers with Strings
&lt;/h3&gt;

&lt;p&gt;In C, a string is implemented as a &lt;strong&gt;null-terminated characters array&lt;/strong&gt;. To work with strings using pointers, you can declare a pointer variable that points to the first character in the string. For instance, the syntax as follows would be used to declare a pointer that points to a string literal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The memory address of the first character, &lt;code&gt;h&lt;/code&gt;, in the string &lt;code&gt;hello&lt;/code&gt; is now stored in &lt;code&gt;str&lt;/code&gt;. To access the characters in the string, just use &lt;code&gt;*&lt;/code&gt; operator. Dereferencing is accomplished via the &lt;code&gt;*&lt;/code&gt; operator. For instance, you could apply the following syntax to print the string's first character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the character &lt;code&gt;h&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the above example, str is a &lt;strong&gt;string literal&lt;/strong&gt; instead of a &lt;strong&gt;string array&lt;/strong&gt;. They are different because string literals are string constants, which cannot be changed, and are stored in the global's read-only memory. &lt;/p&gt;

&lt;p&gt;✅ If you want to modify the characters in the string, you can declare &lt;code&gt;str&lt;/code&gt; as a string array instead of a string literal with code like the following examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or alternatively,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sc"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'l'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'l'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pointers with Arrays of Integers
&lt;/h3&gt;

&lt;p&gt;Arrays in C are stored in a contiguous block of memory. You can declare a pointer variable that refers to the array's first element to work with arrays via pointers. For example, the following syntax would be used to declare a pointer that points to an array of integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;code&gt;ptr&lt;/code&gt; contains the memory address of the first element in the array. You can use pointer arithmetic to access the other elements in the array. For example, to print the second element in the array, you would use the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the value 2, which is the second element in the array.&lt;/p&gt;

&lt;p&gt;💎 &lt;strong&gt;Bonus question&lt;/strong&gt;: how to print the fourth element? Here is the solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pointers with Nested Array of Strings
&lt;/h3&gt;

&lt;p&gt;An array that comprises other arrays is referred to as a &lt;strong&gt;nested array&lt;/strong&gt; in C. You can declare a pointer variable that points to the first element in the outer array to work with nested arrays using pointers. For example, the following syntax would be used to declare a pointer that points to a nested array of strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"world"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"I'm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jenny"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"from"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"UofT"&lt;/span&gt;&lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this two dimensional array called &lt;code&gt;arr&lt;/code&gt;, it is an array of pointers to characters.&lt;/p&gt;

&lt;p&gt;The first set of empty square brackets indicates that &lt;code&gt;arr&lt;/code&gt; is an array with &lt;em&gt;some&lt;/em&gt; number of rows. Actually, the number of rows will be inferred by the compiler when you give it an initial value, which is &lt;code&gt;3&lt;/code&gt; in this case. (Thanks to the compiler, we can use this short-cut to omit the array size.)&lt;/p&gt;

&lt;p&gt;The second set of square brackets specifies the number of columns, which is &lt;code&gt;2&lt;/code&gt; in this case.&lt;/p&gt;

&lt;p&gt;After we initialize the array with three rows and two columns, the first row contains the strings &lt;code&gt;"hello"&lt;/code&gt; and &lt;code&gt;"world"&lt;/code&gt;, the second row contains the strings &lt;code&gt;"I'm"&lt;/code&gt; and &lt;code&gt;"Jenny"&lt;/code&gt;, and the third row contains the strings &lt;code&gt;"from"&lt;/code&gt; and &lt;code&gt;"UofT"&lt;/code&gt;, here, each element is a string literal (i.e., char *).&lt;/p&gt;

&lt;p&gt;Notice that each row of the array &lt;code&gt;arr&lt;/code&gt; is an array of pointer to char. This is because each row of &lt;code&gt;arr&lt;/code&gt; contains a pointer to a one-dimensional array of length two, a pointer to an array of strings is effectively a pointer to the first string of that array of strings, and a pointer to a string is actually the memory address of that string.&lt;/p&gt;

&lt;p&gt;Then, we declare the variable &lt;code&gt;ptr&lt;/code&gt;, which is a pointer to a two-dimensional character array, it is initialized to point to the array &lt;code&gt;arr&lt;/code&gt;. So ptr is an alias of &lt;code&gt;arr&lt;/code&gt;, because they store the same memory address.&lt;/p&gt;

&lt;p&gt;The memory address of the first element of the outer array is now stored in ptr. To access the inner arrays and their elements, use pointer arithmetic and dereferencing. For example, you would use this syntax to output the second string in the first inner array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of code will print the string &lt;code&gt;"world"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at the expression &lt;code&gt;*(*ptr + 1)&lt;/code&gt;. First of all, &lt;code&gt;*ptr&lt;/code&gt; dereferences &lt;code&gt;ptr&lt;/code&gt; to obtain the two-dimensional array &lt;code&gt;arr&lt;/code&gt;, and then, &lt;code&gt;*ptr + 1&lt;/code&gt; is the memory address of the second element in the first row of the &lt;code&gt;arr&lt;/code&gt; array, and then with &lt;code&gt;*(*ptr + 1)&lt;/code&gt;, we dereference the pointer to get the string literal stored at that address, which is &lt;code&gt;"world"&lt;/code&gt;. (More about pointer arithmetic at the later part of the blog, stay tuned 😊)&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Pointers as Parameters to Functions
&lt;/h3&gt;

&lt;p&gt;Pointers are a type of parameter that functions in C can accept. This enables the function to make changes to the original variable outside the function. A function must be declared to accept a pointer argument in order to accept a pointer as a parameter. As an illustration, you would use the following syntax to send a pointer to an integer variable to the increment function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function takes a pointer to an integer as a parameter, and then it increments the value stored at the memory location pointed to by this pointer named &lt;code&gt;ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By giving a pointer to an integer variable, you can invoke the function. For example, the following syntax could be applied to increase the value of the integer &lt;code&gt;num&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print out the value &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing Arrays as Parameters to Functions
&lt;/h3&gt;

&lt;p&gt;Arrays can also be passed as parameters to functions in C. In this case, you effectively send a pointer to the array's first element when you pass an array to a function. For example, the following syntax would be used to pass an array of integers to the sum function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function accepts an array of integers and the size of the array as inputs and returns the sum of the elements.&lt;/p&gt;

&lt;p&gt;You can call the function by passing an array of integers and the size of the array. For example, the following code computes the sum of an array of integers named &lt;code&gt;nums&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the value &lt;code&gt;15&lt;/code&gt;, which is the sum of the values in the &lt;code&gt;nums&lt;/code&gt; array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Usage of Pointers
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Pointers Arithmetic
&lt;/h2&gt;

&lt;p&gt;As you have seen earlier, you can perform arithmetic operations on pointers thanks to the powerful pointer arithmetic capability of pointers in C. You can access an array's elements using pointer syntax and traverse through them using pointer arithmetic. The size of the data type that the pointer is pointing to determines how pointer arithmetic works in C.&lt;/p&gt;

&lt;p&gt;Think about the following code, for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, &lt;code&gt;ptr&lt;/code&gt; points to the first integer of the &lt;code&gt;arr&lt;/code&gt; array. To access the second integer of the array using pointer arithmetic, you can use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;ptr&lt;/code&gt; is incremented by one, which moves the pointer to the memory location of the second element in the array. What is actually added to &lt;code&gt;ptr&lt;/code&gt; is &lt;code&gt;1 * sizeof(int)&lt;/code&gt;, so if an integer takes four bytes in your computer, then actually four bytes are added to the memory address stored at &lt;code&gt;ptr&lt;/code&gt;. By moving ahead &lt;code&gt;4 * sizeof(int)&lt;/code&gt;, now &lt;code&gt;ptr&lt;/code&gt; is storing the memory address of the second integer. The &lt;code&gt;*&lt;/code&gt; operator is used to dereference the pointer and access the value stored at that memory location.&lt;/p&gt;

&lt;p&gt;Note that you can use the array notation to fetch the second element as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Really, by &lt;code&gt;ptr[1]&lt;/code&gt;, what is happening behind the hood is that &lt;code&gt;*(ptr + 1)&lt;/code&gt; is evaluated. You can substitute any other number &lt;code&gt;n&lt;/code&gt; that is in-bound of the size of the array to replace &lt;code&gt;1&lt;/code&gt;. For example, you can write the following line of code to fetch the third element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is equivalent to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use pointer arithmetic to print out each element while iterating through an array. For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using pointer arithmetic, this code iterates through the &lt;code&gt;arr&lt;/code&gt; array and outputs each element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pointers to Pointers
&lt;/h3&gt;

&lt;p&gt;In C, pointers can also be used to hold another pointer's memory address. They are also referred to as &lt;strong&gt;double pointers&lt;/strong&gt; or &lt;strong&gt;pointers to pointers&lt;/strong&gt;. When you need to change a pointer variable inside a function or make a dynamic two-dimensional array, pointers to pointers come in handy.&lt;/p&gt;

&lt;p&gt;You can use the following syntax to declare a pointer to another pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;pt_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This declares the pointer variable &lt;code&gt;pt_ptr&lt;/code&gt;, which can to hold the memory address of another pointer to integer. &lt;/p&gt;

&lt;p&gt;Think about the following code, for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;pt_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;pt_ptr&lt;/code&gt; is a pointer that stores the memory address of the pointer variable, which points to the integer &lt;code&gt;num&lt;/code&gt;. You can access the value stored at the memory location pointed to by &lt;code&gt;pt_ptr&lt;/code&gt; using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;pt_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;*&lt;/code&gt; operator is used twice to dereference the pointer to access the value stored at the memory location pointed to indirectly by &lt;code&gt;pt_ptr&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The first time you dereference &lt;code&gt;pt_ptr&lt;/code&gt; with &lt;code&gt;* pt_ptr&lt;/code&gt;, you can get the memory address of &lt;code&gt;num&lt;/code&gt;, the second time you dereference &lt;code&gt;* pt_ptr&lt;/code&gt; by saying &lt;code&gt;** pt_ptr&lt;/code&gt;, you can now get the value stored in &lt;code&gt;num&lt;/code&gt;, which is &lt;code&gt;9&lt;/code&gt;, therefore &lt;code&gt;9&lt;/code&gt; is then assigned to &lt;code&gt;value&lt;/code&gt;, which is a variable of type &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The C programming language has a powerful feature called pointers that lets you work with memory directly. We explored pointers in C programming and provided examples of how to use them with strings, integer arrays, nested arrays of strings, and nested arrays of arrays of numbers in this blog post. We also covered how to pass arrays as arguments and how to use pointers as parameters to functions. The use of pointers to pointers and pointer arithmetic was also covered, along with examples. You can create more effective and powerful applications by grasping how pointers operate in C programming.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>c</category>
      <category>backend</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
