<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.brandon-rodriguez.com/index.php?action=history&amp;feed=atom&amp;title=Programming%2FC%2FSyntax</id>
	<title>Programming/C/Syntax - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.brandon-rodriguez.com/index.php?action=history&amp;feed=atom&amp;title=Programming%2FC%2FSyntax"/>
	<link rel="alternate" type="text/html" href="https://wiki.brandon-rodriguez.com/index.php?title=Programming/C/Syntax&amp;action=history"/>
	<updated>2026-05-07T13:42:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.brandon-rodriguez.com/index.php?title=Programming/C/Syntax&amp;diff=758&amp;oldid=prev</id>
		<title>Brodriguez: Brodriguez moved page C/Syntax to Programming/C/Syntax</title>
		<link rel="alternate" type="text/html" href="https://wiki.brandon-rodriguez.com/index.php?title=Programming/C/Syntax&amp;diff=758&amp;oldid=prev"/>
		<updated>2023-04-21T03:57:30Z</updated>

		<summary type="html">&lt;p&gt;Brodriguez moved page &lt;a href=&quot;/C/Syntax&quot; class=&quot;mw-redirect&quot; title=&quot;C/Syntax&quot;&gt;C/Syntax&lt;/a&gt; to &lt;a href=&quot;/Programming/C/Syntax&quot; title=&quot;Programming/C/Syntax&quot;&gt;Programming/C/Syntax&lt;/a&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 03:57, 21 April 2023&lt;/td&gt;
				&lt;/tr&gt;
&lt;!-- diff cache key dev_wiki:diff::1.12:old-594:rev-758 --&gt;
&lt;/table&gt;</summary>
		<author><name>Brodriguez</name></author>
	</entry>
	<entry>
		<id>https://wiki.brandon-rodriguez.com/index.php?title=Programming/C/Syntax&amp;diff=594&amp;oldid=prev</id>
		<title>Brodriguez: Create page</title>
		<link rel="alternate" type="text/html" href="https://wiki.brandon-rodriguez.com/index.php?title=Programming/C/Syntax&amp;diff=594&amp;oldid=prev"/>
		<updated>2021-02-01T23:39:34Z</updated>

		<summary type="html">&lt;p&gt;Create page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== Comments ==&lt;br /&gt;
=== Inline Comments ===&lt;br /&gt;
 // This is an inline comment.&lt;br /&gt;
&lt;br /&gt;
=== Block Comments ===&lt;br /&gt;
 /**&lt;br /&gt;
  * This is a block comment.&lt;br /&gt;
  * Comment line 2.&lt;br /&gt;
  * Another block comment line.&lt;br /&gt;
  */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Variables ==&lt;br /&gt;
{{ todo | Expand region. Ref https://en.wikipedia.org/wiki/C_data_types and https://www.tutorialspoint.com/c_standard_library/limits_h.htm }}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Pointers ==&lt;br /&gt;
Unlike most other languages, C gives access to pointers.&amp;lt;br&amp;gt;&lt;br /&gt;
A &amp;quot;pointer&amp;quot; is effectively &amp;quot;the memory address of the respective variable&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In short:&lt;br /&gt;
* {{ ic|&amp;amp;}} - If put before a variable, will give the memory address the variable resides at.&lt;br /&gt;
* {{ ic|*}} - If put before a variable, will interpret as a memory address and give the value residing at said location.&lt;br /&gt;
&lt;br /&gt;
=== Pointer Explanation ===&lt;br /&gt;
The purpose of a pointer is to simplify data in memory being passed around.&amp;lt;br&amp;gt;&lt;br /&gt;
That is, if you have a large data structure, rather than copying or moving that data structure around (as you call functions, etc), it can be cheaper/easier to just pass the memory address that the data structure exists at.&lt;br /&gt;
&lt;br /&gt;
In all the following examples, we use integers, but the same logic applies to every data type there is.&lt;br /&gt;
&lt;br /&gt;
For example, we may have this:&lt;br /&gt;
 // Declare our int.&lt;br /&gt;
 int my_int_var = 5;&lt;br /&gt;
 &lt;br /&gt;
 // Get pointer to variable.&lt;br /&gt;
 int* my_int_ptr = &amp;amp;my_int_var;&lt;br /&gt;
 &lt;br /&gt;
 // Change variable value, using pointer.&lt;br /&gt;
 my_int_ptr* = 10&lt;br /&gt;
&lt;br /&gt;
When we pass the above pointer around, we are telling our program &amp;quot;this is the memory address to look for our integer&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then use the pointer to reference our original value with the following:&lt;br /&gt;
 // Print out pointer.&lt;br /&gt;
 printf(&amp;quot;Value for &amp;quot;my_int_var&amp;quot; is %i\n&amp;quot;, *my_inv_ptr);&lt;br /&gt;
 &lt;br /&gt;
 // Print out direct value. This will give the same as our pointer.&lt;br /&gt;
 printf(&amp;quot;Value for &amp;quot;my_int_var&amp;quot; is %i\n&amp;quot;, my_inv_var);&lt;br /&gt;
&lt;br /&gt;
Finally, note that we can have pointers to pointers. So still referring to the above code, we may have:&lt;br /&gt;
 // Create pointer to pointer.&lt;br /&gt;
 int* my_int_ptr2 = &amp;amp;my_int_ptr;&lt;br /&gt;
 &lt;br /&gt;
 // Print out direct value. This will give the same as our pointer.&lt;br /&gt;
 printf(&amp;quot;Value for &amp;quot;my_int_var&amp;quot; is %i\n&amp;quot;, **my_inv_ptr2);&lt;br /&gt;
&lt;br /&gt;
Note that there is no limit to how deep pointers can nest. So it&amp;#039;s possible to have pointers to pointers to pointers to....for infinity.&lt;br /&gt;
&lt;br /&gt;
=== Malloc &amp;amp; Calloc ===&lt;br /&gt;
Sometimes, a programmer will know they&amp;#039;ll need a pointer in the future, without having a value yet. Such as maybe getting input from a user.&lt;br /&gt;
&lt;br /&gt;
In such cases, we can use the {{ ic |malloc()}} and {{ ic |calloc()}} functions. These two both allocate memory, but calloc() will set the associated memory bits to &amp;quot;0&amp;quot; on allocation.&lt;br /&gt;
&lt;br /&gt;
Note that any time malloc or calloc is used, the {{ ic |free()}} function should be called once the variable is no longer in use. Otherwise, memory leaks will occur as the program runs.&lt;br /&gt;
&lt;br /&gt;
Malloc Example:&lt;br /&gt;
 // Create pointer with malloc.&lt;br /&gt;
 int* my_value = malloc(1 * sizeof(int));&lt;br /&gt;
 &lt;br /&gt;
 // Free pointer (to prevent memory leaks).&lt;br /&gt;
 free(my_value);&lt;br /&gt;
&lt;br /&gt;
Calloc Example:&lt;br /&gt;
  // Create pointer with malloc.&lt;br /&gt;
 int* my_value = calloc(1, sizeof(int));&lt;br /&gt;
 &lt;br /&gt;
 // Free pointer (to prevent memory leaks).&lt;br /&gt;
 free(my_value);&lt;br /&gt;
 &lt;br /&gt;
In both instances, note that we specify both the size of each object, and the number of objects to reference. In both cases, we are saying &amp;quot;I want a pointer to exactly one integer&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
If we instead want &amp;quot;a pointer to memory that contains 10 different integers&amp;quot;, then we can instead have the following:&lt;br /&gt;
 // Create pointer with malloc.&lt;br /&gt;
 int* my_value = calloc(10, sizeof(int));&lt;br /&gt;
 &lt;br /&gt;
 // Reference, say, the 5th integer in our set.&lt;br /&gt;
 my_value[5] = 10;&lt;br /&gt;
 &lt;br /&gt;
 // Free pointer (to prevent memory leaks).&lt;br /&gt;
 free(my_value);&lt;br /&gt;
&lt;br /&gt;
=== Why Have Nested Pointers ===&lt;br /&gt;
Given the above examples, pointers can be thought of in two ways:&lt;br /&gt;
1) A pointer can be thought of as a reference to a memory address.&lt;br /&gt;
2) A pointer can be thought of as a reference to an array.&lt;br /&gt;
&lt;br /&gt;
So theoretically, with if you need a &amp;quot;a pointer to an array&amp;quot;? Or if you need &amp;quot;an array that contains pointers&amp;quot;? Or even just a &amp;quot;multi-dimensional array (2d, 3d, etc)&amp;quot;?&lt;br /&gt;
&lt;br /&gt;
In these cases, we would want to have nested pointers, to suite our needs.&lt;br /&gt;
{{ Note | For all three of the below examples, the syntax to reference values is still the same &amp;quot;nested pointer&amp;quot; syntax. The only thing that changes is how you should mentally imagine the data structure.}}&lt;br /&gt;
&lt;br /&gt;
For example, a pointer to an array may look like:&lt;br /&gt;
 // Create array of 20 ints, via pointer syntax.&lt;br /&gt;
 int* my_array = calloc(20, sizeof(int));&lt;br /&gt;
 &lt;br /&gt;
 // Create pointer to array.&lt;br /&gt;
 int** arr_pointer = &amp;amp;my_array;&lt;br /&gt;
&lt;br /&gt;
If we wanted an array of pointers, then we might have:&lt;br /&gt;
 // Create array with 20 different int pointers.&lt;br /&gt;
 int** my_array = calloc(20, sizeof(int*));&lt;br /&gt;
 &lt;br /&gt;
 // Create associated pointers.&lt;br /&gt;
 for (int index = 0; index &amp;lt; 20; index++) {&lt;br /&gt;
     my_array[index] = calloc(1, sizeof(int*));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we wanted a 2-D array, then we could use pointer syntax to have:&lt;br /&gt;
 // Create array with 20 different 1-D rows.&lt;br /&gt;
 int** my_array = calloc(20, sizeof(int*));&lt;br /&gt;
 &lt;br /&gt;
 // Loop through and create associated rows.&lt;br /&gt;
 for (int index = 0; index &amp;lt; 20; index++) {&lt;br /&gt;
     // Each 1-D row can be whatever size we want. In this case, 10 values long.&lt;br /&gt;
     my_array[index] = calloc(10, sizeof(int));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== If Statements ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;/div&gt;</summary>
		<author><name>Brodriguez</name></author>
	</entry>
</feed>