<?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: Noah11012</title>
    <description>The latest articles on DEV Community by Noah11012 (@noah11012).</description>
    <link>https://dev.to/noah11012</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%2F49612%2Fbc88fb8b-6f81-4e98-8c14-c0f300f22470.png</url>
      <title>DEV Community: Noah11012</title>
      <link>https://dev.to/noah11012</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/noah11012"/>
    <language>en</language>
    <item>
      <title>Using SDL2: Events</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Tue, 18 Aug 2020 23:34:13 +0000</pubDate>
      <link>https://dev.to/noah11012/using-sdl2-events-2il3</link>
      <guid>https://dev.to/noah11012/using-sdl2-events-2il3</guid>
      <description>&lt;p&gt;Games by definition need the quality of being interactive in order to be considered one. A video game typically is interacted through input devices like a joystick or gamepad on consoles and on PCs keyboards and mice are the preferred option. Keys are pressed, the mouse is moved and buttons are clicked.&lt;/p&gt;

&lt;p&gt;All of these aforementioned actions produce what are called events. In SDL and like many other graphical libraries the way you find out what keys are pressed, the coordinates of the mouse and what, if any, mouse buttons are being clicked is by polling for events.&lt;/p&gt;

&lt;p&gt;Internally, SDL has a queue or more specific an event queue. If you don't know what a queue is, you can more or less think about it as a line of people at the checkout. The first people to enter the line are the first people served. We call this a FIFO data structure or First In First Out.&lt;/p&gt;

&lt;p&gt;As the user interacts more with the keyboard and mouse the more the queue is filled with events. Adding to the queue is called enqueuing and the opposite is called dequeuing where you take items off the queue or polling as it is referred to in SDL.&lt;/p&gt;

&lt;p&gt;To poll events off the queue, use the &lt;code&gt;SDL_PollEvent&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The only argument it takes is a pointer to an &lt;code&gt;SDL_Event&lt;/code&gt; structure which is basically an &lt;code&gt;enum&lt;/code&gt; plus a &lt;code&gt;union&lt;/code&gt;. For any type of event that SDL sends to the queue and can be polled by the programmer there is a corresponding event structure that is placed in the &lt;code&gt;union&lt;/code&gt; of &lt;code&gt;SDL_Event&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if the event's type is equal to the &lt;code&gt;enum&lt;/code&gt; constant &lt;code&gt;SDL_MOUSEMOTION&lt;/code&gt; this means the mouse has moved within confines of the window and can be accessed through the structure &lt;code&gt;SDL_MouseMotionEvent&lt;/code&gt; to get the relevant information that the programmer needs. The SDL documentation provides all possible events that can be generated here: &lt;a href="https://wiki.libsdl.org/SDL_Event"&gt;https://wiki.libsdl.org/SDL_Event&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because it is possible to have multiple events on the queue at any given time, it is desirable to continuously poll the queue until we exhaust it. &lt;code&gt;SDL_PollEvent&lt;/code&gt; returns one if we still have events on the queue and zero if empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;SDL2/SDL.h&amp;gt;
&lt;/span&gt;
&lt;span class="cm"&gt;/* Helper function to convert the number mapped to the buttons
   on the mouse to a readable string. This function does
   not handle all possible mouse buttons. */&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;convert_button_number_to_string&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;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"NO_BUTTON"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_BUTTON_LEFT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"left"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_BUTTON_MIDDLE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"middle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_BUTTON_RIGHT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"right"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nl"&gt;default:&lt;/span&gt;
        &lt;span class="k"&gt;break&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;result&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SDL_Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SDL_INIT_EVERYTHING&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;SDL_Window&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SDL_CreateWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SDL2 Window"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                          &lt;span class="n"&gt;SDL_WINDOWPOS_UNDEFINED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                          &lt;span class="n"&gt;SDL_WINDOWPOS_UNDEFINED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                          &lt;span class="mi"&gt;1280&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                          &lt;span class="mi"&gt;720&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="n"&gt;SDL_Renderer&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;renderer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SDL_CreateRenderer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&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;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;SDL_Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;running&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SDL_PollEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_QUIT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Quitting application&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_MOUSEMOTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Mouse Position = { "&lt;/span&gt;
                          &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;motion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;
                          &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;motion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" }&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_MOUSEBUTTONDOWN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_MOUSEBUTTONUP&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;convert_button_number_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                          &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" mouse button "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;SDL_PRESSED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"pressed"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"released"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_KEYDOWN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SDL_KEYUP&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Key "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keysym&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;
                          &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;SDL_PRESSED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"pressed"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"released"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&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="n"&gt;SDL_RenderPresent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;SDL_DestroyWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;SDL_Quit&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;Above is something to help you get started with SDL events. It only handles very basic events like closing of the application, key presses and mouse events like movement and use of the mouse buttons.&lt;/p&gt;

&lt;p&gt;To find this and the rest of the code for the series up to this point and in the future go to the SDL2 Tutorial Code repository: &lt;a href="https://github.com/Noah11012/sdl2-tutorial-code"&gt;https://github.com/Noah11012/sdl2-tutorial-code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I'll see you all in my next article!&lt;/p&gt;

</description>
      <category>sdl2</category>
      <category>c</category>
      <category>events</category>
    </item>
    <item>
      <title>String Internalization</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Sun, 24 Feb 2019 03:31:53 +0000</pubDate>
      <link>https://dev.to/noah11012/string-internalization-39jg</link>
      <guid>https://dev.to/noah11012/string-internalization-39jg</guid>
      <description>&lt;p&gt;Many modern interpreted programming languages like Python and Lua have a builtin feature that allows for O(1) comparisons of strings. Ordinarily, comparing two strings is a O(min(x, y)) operation that for huge strings can cause slow downs in performance. Compilers and interpreters can utilize this to their advantage to improve performance when checking against keywords and such. This will hopefully allow the programmer to focus on the more important tasks on hand like lexing, parsing, and generation of assembly instructions or byte code.&lt;/p&gt;

&lt;p&gt;How does string internalization work? Like all other problems when related to the computer and the world, everything has a trade off. The two main ones are efficiency and memory usage and consumption. Some solution to a problem may have a small footprint when it comes to memory but might be slow comparatively to something that uses more memory but is more efficient. There are a few ways we can go about to internalize a string but we will do it how I have been doing it.&lt;/p&gt;

&lt;p&gt;First, we need a data structure to store the strings themselves and to solve that we will use a stretchy buffer. If you don't know, they were invented by Sean Barrett and is similar to C++'s std::vector. Because this article is dealing with string internalization, we will not focus on the implementation details of a stretchy buffer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// If buffer is NULL, create it and push the item.&lt;/span&gt;
&lt;span class="c1"&gt;// If buffer is full, reallocate and push the item.&lt;/span&gt;
&lt;span class="cp"&gt;#define buffer_push(buffer, item) ...
&lt;/span&gt;
&lt;span class="c1"&gt;// Gets the length of a stretchy buffer.&lt;/span&gt;
&lt;span class="cp"&gt;#define buffer_length(buffer) ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When it comes time to compare two internalized strings, what is actually being compared? Well, in our version, it is the memory address of the strings. If both are the same, then the strings are the same, if not, then the strings are not the same.&lt;/p&gt;

&lt;p&gt;When we internalize a string, we will have to search the list and compare each entry against the one we are trying to internalize. This is the price we pay, but in the long is worth it as you can hold onto the memory address long as you want to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Where all internalized strings will be stored. */&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;inter_strings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;string_internalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Search if the string already exists in the list. */&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="k"&gt;while&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;buffer_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inter_strings&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* If found, return the internalized string. */&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inter_strings&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;inter_strings&lt;/span&gt;&lt;span class="p"&gt;;&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;span class="cm"&gt;/* If not found, create internalized copy and return it. */&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;new_string&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;string_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;new_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_len&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;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="n"&gt;memory_copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string_len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;buffer_push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inter_strings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_string&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;new_string&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 is definitely a short one, but thought it was something that might be helpful.&lt;/p&gt;

</description>
      <category>string</category>
      <category>internalization</category>
    </item>
    <item>
      <title>C++ References</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Tue, 05 Feb 2019 01:37:25 +0000</pubDate>
      <link>https://dev.to/noah11012/c-references-5ghm</link>
      <guid>https://dev.to/noah11012/c-references-5ghm</guid>
      <description>&lt;p&gt;C++ is backward compatible with C so it inherited pointers. A quick recap if you don't know what a pointer is: a pointer is a variable that holds the memory address of an object. Declaring a pointer involves the type of object we are pointing to and the symbol '*'.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&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="mi"&gt;10&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_value&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;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;vaule2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;506.305&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr_value2&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;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have our pointer, but how do we get an object's memory address? With the ampersand symbol (&amp;amp;).&lt;/p&gt;

&lt;p&gt;To access data through a pointer, we need to do what is called dereferencing. This again includes the use of the * symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&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;"value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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_value&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;"value2: %f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;, *ptr_value2);&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that the recap is over, we can move onto references.&lt;/p&gt;

&lt;p&gt;C++ introduced references as an alternative to pointers. References were designed to be simpler, safer, and easier to read when in use. To create a reference is similar to how we declare a pointer. First, we need the type and the symbol '&amp;amp;'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&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="mi"&gt;200&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;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ref_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice we don't have anything extra on the right-hand side as we had with pointers. The only extra symbol in this statement is the &amp;amp; symbol on the left.&lt;/p&gt;

&lt;p&gt;Another benefit is obvious when it is time to retrieve value from the reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ref_value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are no extra symbols cluttering the code. Viewing small examples of dereferenced pointers may not show the cognitive burden that may be placed on someone when viewing a larger block of code that has several dereferences going on in a short amount of time.&lt;/p&gt;

&lt;p&gt;References, as aforementioned, are safer. Going back to pointers for a moment, a pointer can be within a state of "invalidness". This could mean two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The pointer is declared but has no initializer and is subsequently default-initialized&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Within local scope, if a variable is not provided with an initial value at creation, a garbage value is left there. Most likely, it was some value left over from some memory previously that we are now occupying. Accessing a default-initialized object is undefined behavior and dereferencing a default-initialized pointer is also undefined behavior.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Null Pointers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A null pointer is a special kind of pointer. Usually, it is a way to say, "Hey, I'm not in use right now, but I am still explicitly initialized!". Null pointers have a place in API design. Many C functions that return a pointer may instead return a null pointer on error. Despite it being "safer" than the previous type of invalid pointer, dereferencing a null pointer is undefined behavior.&lt;/p&gt;

&lt;p&gt;At the creation of a reference, it must have an initial value to bind. Failure to do so is a compile time error. Also, a reference is bound to one value for its entire lifetime.&lt;/p&gt;

&lt;p&gt;However, references have their weaknesses, too. Like pointers, a reference can "dangle": referring to a space of memory that is freed.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;return_five&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&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;some_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;return_five&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;some_value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// undefined behavior&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;return_five()&lt;/code&gt; is a function that creates a variable and returns it. This may appear innocent to the inexperienced but this program is in error. This value being returned is created on the stack and when the function returns, the stack frame is destroyed and so is the variable inside.&lt;/p&gt;

&lt;p&gt;By the time we return the main function, the reference &lt;code&gt;some_value&lt;/code&gt; is now referring to freed memory and is a dangling reference.&lt;/p&gt;

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

&lt;p&gt;References, in theory, are a better alternative to pointers in some regards. Providing a simpler syntax at creation and accessing bounded values. The restraints in place eliminate some of the problems that using pointers may arise.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>references</category>
    </item>
    <item>
      <title>Socket Programming in C: Communication Styles</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Sat, 26 Jan 2019 14:35:28 +0000</pubDate>
      <link>https://dev.to/noah11012/socket-programming-in-c-communication-styles-27fc</link>
      <guid>https://dev.to/noah11012/socket-programming-in-c-communication-styles-27fc</guid>
      <description>&lt;p&gt;Being there are multiple different types of sockets available for our use, we will take a brief look at some of them. When we get to socket creation, the second argument takes a number that represents the communication style. Each number has a symbolic constant.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. SOCK_STREAM
&lt;/h2&gt;

&lt;p&gt;A socket that streams data over a connection like a cable or a pipe. It transmits data like a stream of bytes reliably.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. SOCK_DGRAM
&lt;/h2&gt;

&lt;p&gt;This style of sockets doesn't have a connection but instead, each packet is addressed and sent individually. Also, this an unreliable style of communication unlike SOCK_STREAM. Each data written to this kind of socket is converted into one packet. The only guarantee this socket gives you is it will try its best to transmit every packet you send.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. SOCK_RAW
&lt;/h2&gt;

&lt;p&gt;Most user-level programs will not use this kind of communication as it provides low-level access to protocols and interfaces.&lt;/p&gt;

</description>
      <category>c</category>
      <category>sockets</category>
      <category>networking</category>
    </item>
    <item>
      <title>Small Patterns and Idioms in C</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Sun, 20 Jan 2019 17:57:17 +0000</pubDate>
      <link>https://dev.to/noah11012/small-patterns-and-idioms-in-c-47d1</link>
      <guid>https://dev.to/noah11012/small-patterns-and-idioms-in-c-47d1</guid>
      <description>&lt;p&gt;Patterns are constructs that programmers use to solve a general recurring problem. If a problem has deviated from the original, the pattern can be adapted to still be effective. In this article, we will learn a couple of them for the C programming language that I believe beginners and intermediates should know and start to apply them where ever possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Do-While Loop in Macros
&lt;/h2&gt;

&lt;p&gt;If you need to use a temporary variable to store a value that will shortly be used in a macro, then it will "leak" into the local scope where it is used. The user will no longer be able to reuse the variable's name and if they do, the compiler will complain of a "redefinition". Some programmers solve this by giving a name that nobody else would use. Usually, this is a hash prefixed/suffixed onto the variable's name. The better way, in my opinion, is to use a do-while loop. When the loop goes out of scope, so do the variables and there is no "leakage". A do-while loop will always run at least once even if the &lt;code&gt;while&lt;/code&gt;'s condition is false.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#define swap(type, a, b)  \  
&lt;/span&gt;    &lt;span class="k"&gt;do&lt;/span&gt;                    &lt;span class="err"&gt;\&lt;/span&gt;  
    &lt;span class="p"&gt;{&lt;/span&gt;                     &lt;span class="err"&gt;\&lt;/span&gt;  
        &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="err"&gt;\&lt;/span&gt;  
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="err"&gt;\&lt;/span&gt;  
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="err"&gt;\&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&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="err"&gt;\&lt;/span&gt;  

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

    &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&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;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&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;"Swapped values:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;a = %d, b = %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;h2&gt;
  
  
  2. Unions and Enums
&lt;/h2&gt;

&lt;p&gt;Before we move on, let's make sure all of us understand what a union is. A union is like a structure but with one stipulation: only one member can be used at a time. You can think unions as types that change depending on what type of value we want to store. An example of using a union can found when processing tokens in a compiler/interpreter. Some tokens may be an integer, character, or string. Perfect place for a union. But there is one problem: the union doesn't give us the information about what type is currently in use. That's where &lt;code&gt;enum&lt;/code&gt;s come in. An &lt;code&gt;enum&lt;/code&gt; or enumeration is a list of names that map to a number. The first member is, by default, equal to zero and subsequent values are one plus the previous value.  One of the uses of an enumeration is listing the possible values. In our case, this would be the list of possible tokens that we can accept.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt;  
&lt;span class="p"&gt;{&lt;/span&gt;              
    &lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="n"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="n"&gt;STRING&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;TokenType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;  
&lt;span class="p"&gt;{&lt;/span&gt;          
    &lt;span class="n"&gt;TokenType&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
    &lt;span class="k"&gt;union&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;int_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;char_value&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="n"&gt;string_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every time we encounter a token, we figure out what type it is, set &lt;code&gt;type&lt;/code&gt; to the appropriate value and set the correct member in &lt;code&gt;value&lt;/code&gt;. When the compiler/interpreter comes back to this list of tokens to form an AST or whatever it needs to do, it can look at the &lt;code&gt;type&lt;/code&gt; and do the right operation.&lt;/p&gt;

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

&lt;p&gt;Programmers can use the language's ability for leverage to solve problems that pop up every now and then. Sometimes we see two features paired and used in tandem like in the last entry.&lt;/p&gt;

&lt;p&gt;For now, this is a standalone post but may in the future, close or far, be a series.&lt;/p&gt;

</description>
      <category>c</category>
      <category>patterns</category>
      <category>idioms</category>
    </item>
    <item>
      <title>Socket Programming in C: What is a Socket?</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Wed, 16 Jan 2019 19:35:01 +0000</pubDate>
      <link>https://dev.to/noah11012/socket-programming-in-c-what-is-a-socket-577p</link>
      <guid>https://dev.to/noah11012/socket-programming-in-c-what-is-a-socket-577p</guid>
      <description>&lt;p&gt;This is the post that marks the beginning of this series. A journey, when arrived at the end, will give you control over sockets. No longer will they be disorderly or out of your reach because you will be the one to have dominion over them. Bending their will to yours and serving your every whim.&lt;/p&gt;

&lt;p&gt;People dream to have this kind of power and you can be the one to achieve it only if you read this series to its conclusion.&lt;/p&gt;

&lt;p&gt;Of course, you can still get this kind of power from other great tutorials out on the internet but it would be appreciated if you stuck till the very end.&lt;/p&gt;

&lt;p&gt;Let us begin this journey!&lt;/p&gt;

&lt;p&gt;What is a socket? Simply put, a socket is an endpoint for where the data will be sent to and received. Sockets are used as a generalized means of communication between different processes on the same computer or one on the same network. Because everything in a Unix based OSes is a file or can be represented as one, processes identify sockets by their file descriptor.&lt;/p&gt;

&lt;p&gt;Sockets come in different flavors. Each flavor specifies the guarantees and leniency that is given. From now on this will be known as communication styles. Next, what protocol is used? This is where the low-level details and mechanisms tell how the data is transmitted and received.&lt;/p&gt;

&lt;p&gt;Communication styles can be thought of as a series of questions that are given a different answer depending on the socket's communication style.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Units used?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some styles regard data as simply bytes with no structure whatsoever. Others think data as being grouped into a structure determined by the style.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loss during transmission?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Will the occasional drop of a packet matter overall? Will the packets received on the other end be in the same order as it was sent? Some styles guarantee that all packets transmitted will be retrieved in the same order and that if a packet is dropped, it is resent. Other styles of communication may allow some packets to drop. Packets may also arrive more than once and out of order. Programmers who use such sockets must place safeguards in their code to accommodate for the unreliability of this communication style.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Partner?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The traditional way of thinking a socket is similar to the client-server relationship, where both parties can freely exchange data with each other. There is also a one-sided version: stamp the address and send the data on its way.&lt;/p&gt;

&lt;p&gt;How do we set the endpoint for a socket? Each socket is paired with a socket address, or in other words, the IP address and a port number. Setting a socket's address is called binding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;For the next article, we will discuss the different kinds of communication styles that we can use.&lt;/p&gt;

</description>
      <category>c</category>
      <category>sockets</category>
      <category>networking</category>
    </item>
    <item>
      <title>Socket Programming in C: Introduction</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Tue, 15 Jan 2019 19:24:38 +0000</pubDate>
      <link>https://dev.to/noah11012/socket-programming-in-c-introduction-41nf</link>
      <guid>https://dev.to/noah11012/socket-programming-in-c-introduction-41nf</guid>
      <description>&lt;p&gt;Today I am announcing a new series that is underway. The series will teach socket programming in C and will hopefully provide material that is clear and easy to understand especially for those new to sockets and wanting to learn. Plus, if you stick till the end, a reward in the form of a final project is made that I think you will all enjoy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Learn Socket Programming
&lt;/h2&gt;

&lt;p&gt;In the next post, where I will go into more depth to what a socket is, provides a means of communication between two processes which could be across the world provided they are both on the same network. You may want to learn sockets to do just this. Another reason is simply to learn. Nothing wrong with increasing your knowledge about the world around you and becoming more informed in the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This series assumes its audience has little to none experience with sockets. With that being said, you will still need how to program in C or even C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;The next post will talk more about sockets.&lt;/p&gt;

</description>
      <category>c</category>
      <category>sockets</category>
      <category>networking</category>
    </item>
    <item>
      <title>Reading and Writing Files in C: Part 5</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Mon, 14 Jan 2019 16:24:00 +0000</pubDate>
      <link>https://dev.to/noah11012/reading-and-writing-files-in-c-part-5-3g5e</link>
      <guid>https://dev.to/noah11012/reading-and-writing-files-in-c-part-5-3g5e</guid>
      <description>&lt;p&gt;Just as I promised, we will write and subsequently read back a C structure! One caveat though: you need to know about binary files. You can learn more in the previous article.&lt;/p&gt;

&lt;p&gt;For testing, the following structure will be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;TestStruct&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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&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;First, a connection to a file, specifically, a binary file is needed. Next, we create an instance of &lt;code&gt;TestStruct&lt;/code&gt; and set our desired values. Finally, using &lt;code&gt;fwrite()&lt;/code&gt; to write the structure into the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;TestStruct&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"wb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;TestStruct&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&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;105&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;194.543&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;fwrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&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;TestStruct&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;clang&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; 
&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;VL7&lt;/span&gt;&lt;span class="err"&gt;�&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;Qh&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;                                                                    
&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Trying to output a file that is saved with a C structure results in garbage.&lt;/p&gt;

&lt;p&gt;Let's now read back in the data and see if everything is still correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;TestStruct&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;TestStruct&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;fread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&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;TestStruct&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&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;"ts.a = %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;ts.b = %f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;b&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;clang&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;     
&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&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;105&lt;/span&gt;
&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;194.543000&lt;/span&gt;

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Everything was written and read properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;This is a short article and there is more to explore but I feel everything that was taught in this series will provide many people with adequate knowledge to accomplish basic and more difficult problems relating to files.&lt;/p&gt;

</description>
      <category>c</category>
      <category>io</category>
      <category>file</category>
    </item>
    <item>
      <title>Reading and Writing Files in C: Part 4</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Mon, 14 Jan 2019 00:35:49 +0000</pubDate>
      <link>https://dev.to/noah11012/reading-and-writing-files-in-c-part-4-579n</link>
      <guid>https://dev.to/noah11012/reading-and-writing-files-in-c-part-4-579n</guid>
      <description>&lt;p&gt;For the last three posts, we have only handled text and nothing more. But now we will delve into binary files and how to read from and write to it. I'm sure the first question if don't know what a binary file is, "What is it?". Good question especially for something we are about to use.&lt;/p&gt;

&lt;p&gt;A binary file is a file that isn't intended to be read like text. Instead, they are based more on numerical values.&lt;/p&gt;

&lt;p&gt;When opening a binary file, the mode is additionally set to binary and we do this by simply appending a 'b' to the end.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;binary_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two functions you will be familiarized with are &lt;code&gt;fread()&lt;/code&gt; and &lt;code&gt;fwrite()&lt;/code&gt;. Usually, they are reserved for working with binary files but are as capable of operating on text files. &lt;/p&gt;

&lt;p&gt;Both have the same number of arguments and all but the first have the exact same purpose. The first argument for &lt;code&gt;fwrite()&lt;/code&gt; is where the data will be transferred from to the file whereas in &lt;code&gt;fread()&lt;/code&gt; it is the destination for any data read from a file. Because the first argument's type is a &lt;code&gt;void&lt;/code&gt; pointer, we can receive or transmit any type of data. The second is the size of each object. The third is how many objects we want to read/write. And finally, the fourth and last is the file stream.&lt;/p&gt;

&lt;p&gt;I feel like you now deserve an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"wb"&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;numbers&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;67&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;fwrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;clang&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; 
&lt;span class="n"&gt;ABC&lt;/span&gt;

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you try to open the file that was written with a text editor, most likely it will complain at you with the error "Unknown encoding" or something similar. If you use a terminal command like &lt;code&gt;cat&lt;/code&gt; you will see the contents is A, B, and C.&lt;/p&gt;

&lt;p&gt;Even though we didn't write any text, we used the character's corresponding ASCII value. And if you didn't know, the number 10 is the ASCII value for a newline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;For the next post, we'll leverage them a bit more to read and write entire C structures!&lt;/p&gt;

</description>
      <category>c</category>
      <category>io</category>
      <category>file</category>
    </item>
    <item>
      <title>Function Pointers in C</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Sun, 13 Jan 2019 03:22:51 +0000</pubDate>
      <link>https://dev.to/noah11012/function-pointers-in-c-3ah0</link>
      <guid>https://dev.to/noah11012/function-pointers-in-c-3ah0</guid>
      <description>&lt;p&gt;My last article was about a topic that most beginners found challenging when first confronted: &lt;a href="https://dev.to/noah11012/double-pointers-in-cc-2n96"&gt;double pointers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, in this post, we will take a look at another class of pointers: function pointers&lt;/p&gt;

&lt;p&gt;At their essence, function pointers are like their normal counterparts: a variable that holds the memory address of an object. Function pointers fulfill this role but the object they refer to have a special quality: they are callable. When an object is callable, the operator &lt;code&gt;()&lt;/code&gt; can be used. The only callable objects in C are functions but in C++ some of the additions are classes that overload the call operator.&lt;/p&gt;

&lt;p&gt;The syntax for defining a function pointer is considered the more quirky of the language and some beginners find it difficult to discern. So, we will start with a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;function_pointer&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To better help ourselves learn the syntax of function pointers, we will tease it apart slowly. One thing that pops out immediately is the appearance of braces around &lt;code&gt;*function_pointer&lt;/code&gt;. These parentheses are important and without them it means something entirely different. With the exclusion of the parentheses, it means a function that takes no input and returns a &lt;code&gt;void&lt;/code&gt; pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;function_pointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Another thing that makes a function pointer a pointer is the use of the symbol &lt;code&gt;*&lt;/code&gt;. All pointers have them and function pointers are no different but they are within the braces. Everything else can be read the same. To help with reading function pointers, remove the braces and &lt;code&gt;*&lt;/code&gt; and read it like it is a normal function.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&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;function_pointer&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Remove the braces and &lt;code&gt;*&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;function_pointer&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Cleaner and easier to parse with our eyes! We now know this function pointer points to a function that takes an &lt;code&gt;int *&lt;/code&gt; and a &lt;code&gt;double *&lt;/code&gt; and returns a &lt;br&gt;
 &lt;code&gt;int *&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, like everything else that is learned, what are the uses for function pointers?&lt;/p&gt;

&lt;p&gt;Function pointers can be passed as an argument to another function which will be called within. These function pointers are known as callbacks. For example, let's say we have a function that downloads something from the internet and takes a function pointer. When the download is complete or an error has happened, we would like to execute the callback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;download_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;callback_function&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;status_code&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using our method of stripping away the braces and &lt;code&gt;*&lt;/code&gt; we get a function pointer that refers to a function that accepts an &lt;code&gt;int&lt;/code&gt; but returns nothing.&lt;/p&gt;

&lt;p&gt;Another example of where function pointers can be utilized is event programming. Event programming in its simplest form is the idea when an event happens, do this. A perfect place for function pointers!&lt;/p&gt;

&lt;p&gt;Let's say we have a GUI widget library and we have a function that sets up a callback that is called when a click has been detected on a button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;button_set_click_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;on_click&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm sure by now you can parse what the callback is pointing to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The concept of function pointers is easy to learn, but the syntax isn't the greatest and can leave someone confused when trying to analyze them. I hope this article helped and that any confusion you might have is now gone.&lt;/p&gt;

</description>
      <category>c</category>
      <category>function</category>
      <category>pointers</category>
    </item>
    <item>
      <title>Double Pointers in C/C++</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Sat, 12 Jan 2019 18:14:47 +0000</pubDate>
      <link>https://dev.to/noah11012/double-pointers-in-cc-2n96</link>
      <guid>https://dev.to/noah11012/double-pointers-in-cc-2n96</guid>
      <description>&lt;p&gt;One source of confusion among new C programmers is pointers. At first glance, there doesn't seem to be much usefulness in using them. But, it is crucial to understand pointers as it is a useful tool and any project bigger than a "Hello, World" program will have pointers. Once beginners start to grasp the concept and applicability of pointers, another wave of terror strikes deep into their hearts: double pointers.&lt;/p&gt;

&lt;p&gt;Before moving on any further, let's recap what a pointer is.&lt;/p&gt;

&lt;p&gt;In C and other languages like C++, a pointer is something that holds the memory address of an object. They are a numeric value and when outputted to the console they are usually presented in hexadecimal. This means, essentially, pointers are just fancy integers.&lt;/p&gt;

&lt;p&gt;Now back to double pointers.&lt;/p&gt;

&lt;p&gt;Upon seeing a double pointer, a beginner begins to shift uncomfortably, sweat running down their forehead all the way to their chin. Like everything else in life that is new or foreign, people feel awkward and uneasy even just slightly at the prospect of facing something they have never encountered before. But as you learn more, you start to feel confident in your new understanding of anything you desired to conquer. The same goes with double pointers.&lt;/p&gt;

&lt;p&gt;So, what is a double pointer? Well, if a regular pointer is to refer to an object in memory, then a double pointer is a variable that points to another pointer which in turn, points to an object in memory.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;value_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;value&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;value_double_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;value_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;"Value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&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;"Pointer to value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;value_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;"Double pointer to value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;value_double_ptr&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;clang&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;     
&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="n"&gt;Pointer&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="n"&gt;Doublue&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When dereferencing a double pointer, we do not get the final object, but what is expected: a pointer that must be dereferenced one more time to retrieve the final value. It would be similar to the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&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="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value_double_ptr&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;final_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;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I'm sure that most who are new to double pointers must be asking themselves the question, "Where will I ever use this?". Probably the best way to present the usefulness of double pointers is to remember one of the practical uses of regular pointers. This is to say, regular pointers can be used as an output parameter in functions. An output parameter, if you happen to not know, is a parameter that is filled with a result. Why you would use output parameters is subject to several factors and is beyond the scope of this article but I felt this is where double pointers can be utilized.&lt;/p&gt;

&lt;p&gt;A function that we will take a look at is the POSIX &lt;code&gt;getline()&lt;/code&gt; function. Its purpose is to read one line from a file. When the line is read, one might suspect it to be returned, but, this is not the case as the return value is used for something different. Instead, the first argument, which takes a double pointer, is filled with the buffer that contains the line.&lt;/p&gt;

&lt;p&gt;Why does it take a double pointer? So, that it can allocate enough memory to hold the entire line plus the null terminator character. If memory allocation and reading the one line is successful, then the pointer supplied is modified to point to the new and filled buffer. They could have only achieved this if they were using a double pointer.&lt;/p&gt;

&lt;p&gt;When passing anything to a function, a copy is passed and not the actual object. The same goes for a pointer. If we passed a regular pointer, we could only modify the contents that the pointer points to. If we changed the pointer itself, the change would not reflect outside of the function because it is a copy.&lt;/p&gt;

&lt;p&gt;If we want to change where the pointer points to, we have to add another indirection and take a double pointer.&lt;/p&gt;

&lt;p&gt;An example of using &lt;code&gt;getline()&lt;/code&gt; might be the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt; 
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;   
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&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="n"&gt;line&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="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;line_length&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;getline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;line_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&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;"%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&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;"Line length: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line_length&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;&lt;code&gt;getline()&lt;/code&gt; has two output parameter: one for the buffer and the other for the length of the line that was read in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Double pointers are a challenge to many beginners immediately when first learned. As you become more comfortable with the idea of double pointers and use them when necessary in your own code, you start to think how silly that once you were afraid of double pointers.&lt;/p&gt;

&lt;p&gt;And now I send you off with your new knowlegde!&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>double</category>
      <category>pointers</category>
    </item>
    <item>
      <title>Reading and Writing Files in C: Part 3</title>
      <dc:creator>Noah11012</dc:creator>
      <pubDate>Thu, 10 Jan 2019 02:10:56 +0000</pubDate>
      <link>https://dev.to/noah11012/reading-and-writing-files-in-c-part-3-i4l</link>
      <guid>https://dev.to/noah11012/reading-and-writing-files-in-c-part-3-i4l</guid>
      <description>&lt;p&gt;In the last section of the previous article, I promised that we would discuss some of the different errors that potentially could happen. While it is not necessary to learn about the different errors that could pop up, it is, however, educational and informative to know about the different errors that might come up when working with files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Moved/Deleted
&lt;/h3&gt;

&lt;p&gt;For some programs, a file is used to record the date and time of certain operations committed by the program and errors. If a user or some other program moves the file to another location or deletes it, then write operation, in theory, could fail and the program will (hopefully) recover gracefully. This usually involves recreating the file and writing all new entries that the program chooses to record. This most likely won't happen with files that are shortlived because, well the name implies, will not be around for very long for a user to move or delete. In Windows, if a file is opened by a process, you are not allowed to move or delete it. On Unix based OSes, a move or deletion of a file is considered acting upon the directory where the file resides. If open and deleted, it will remain on disk until closed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Operations After Closing
&lt;/h3&gt;

&lt;p&gt;Each operating system exposes an API for us to get resources that only it can provide. One of these resources is files. On Unix based OSes like Linux, the system call &lt;code&gt;open()&lt;/code&gt; exists. Before allowing us to open the file, the OS must make sure that we have the permission to do so and, of course, exists on the hard drive. Once the OS has concluded that we are not in any violation of any permissions and can locate the file on the disk, it will return an ID for us to use. This ID is a numeric value and in the context of our program is assigned to an opened file. When we're done with the file, we have to let the OS know to close the file. For Unix basic OSes like Linux, &lt;code&gt;close()&lt;/code&gt; does this. In the C standard library file I/O library,  this is &lt;code&gt;fclose()&lt;/code&gt;. Doing any operations with a file that has been closed will result in an error.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&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="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;"Error reading file!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&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;"%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; &lt;span class="n"&gt;clang&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; 

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; 
&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;reading&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Desktop&lt;/span&gt; 
&lt;span class="err"&gt;➜&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Full Storage
&lt;/h3&gt;

&lt;p&gt;If the storage medium that is currently being used is at maximum storage capacity then there isn't much to do except for freeing space up by deleting files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Faulty Hardware
&lt;/h3&gt;

&lt;p&gt;Failing hard drives can and will cause file I/O operations to fail. Because of the unstable nature of a deteriorating storage medium, you may not even get an error from the OS, but is still an error nonetheless because the data that is read/written might be corrupted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;For the next part, we will discuss how to save more than just text in a file.&lt;/p&gt;

</description>
      <category>c</category>
      <category>io</category>
      <category>file</category>
    </item>
  </channel>
</rss>
