    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: String Theory</title>
        <link>https://members.accu.org/index.php/journals/721</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 8, #1 - Feb 1996 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c137/">081</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c137-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c137+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;String Theory</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 February 1996 13:15:26 +00:00 or Sat, 03 February 1996 13:15:26 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>Strings in C are at once both a major convenience and one of the
greatest sources of misunderstanding, and hence of bugs. C is
fairly free and easy with a number of its features, but there can
be a price to pay for living in a free state.</p>
<p>In this series I hope to introduce the fundamentals of C
strings, focus on some of the pitfalls and, hopefully, answer a few
questions.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e26" id="d0e26"></a>Begin at the
beginning</h2>
</div>
<p>So what is a string in C? It is a sequence of non-null
characters terminated by a null character. Although implemented as
an array, a random access data structure, for many purposes we must
treat a string as a sequential data delimited structure. For
example, to determine the length of a string we start at the
zero'th character and move forward until we hit the null.</p>
<p>So, if in principle, a string is an array, albeit one with a
special interpretation, we should be able to create one from
scratch and print it out:</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
int main(void)
{
  char message[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
  puts(message);
  return 0;
}
</pre>
<p>The null delimiter at the end means that we must always remember
that the space occupied by a string is one more than its effective
length. Forgetting this provides one of the most fruitful sources
of errors in C.</p>
<p>Arrays can be declared in C without specifying their length. In
this case an initialiser must be present so the compiler can
determine the length for itself. The following is equivalent to the
code above:</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
int main(void) 
{
  char message[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
  puts(message);
  return 0;
}
</pre>
<p>This is less error prone and eases change:</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
int main(void)
{
  char message[] =
  {
    'h', 'e', 'l', 'l', 'o', ' ',
    'w', 'o', 'r', 'l', 'd', '\0'
  };
  puts(message);
  return 0;
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e45" id="d0e45"></a>Comfortably
null</h2>
</div>
<p>Something worth clearing up before we go any further is the
concept of nullness. In the context I have discussed so far, the
null character is the character with an integer value of zero. In C
this is true regardless of the character set used. The standard way
of writing this is '\0', as above. I could equally well have
written an integer 0, but from the reader's point of view this is
not as clear.</p>
<p>In text you will often see the null character referred to as
NUL. This is its ASCII name, and should not be confused with NULL,
which is the standard C macro for indicating a null pointer -
noting also that NULL is not the same thing as the null pointer.
You should never, ever write:</p>
<pre class="programlisting">
char wrong_headed = NULL;
</pre>
<p>Although it may compile on some systems, this is coincidence and
is not guaranteed. NULL is intended to be used as a pointer, so do
not assume anything else about it - it may be defined as <tt class=
"literal">0</tt>, <tt class="literal">0L</tt> and, in C but not
C++, <tt class="literal">(void*)0</tt>.</p>
<p>In case you are tempted to write the following:</p>
<pre class="programlisting">
#define NUL '\0'
</pre>
<p>Don't bother: it buys you nothing. It is more likely to confuse
others than help them. Using the literal '\0' is far clearer as it
is unambiguous, cannot be redefined, and says the same thing to all
programmers.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e71" id="d0e71"></a>Taking it
literally</h2>
</div>
<p>Clearly, initialising arrays character at a time is tedious.
Recognising the frequent use of strings, there is a convenient
short hand for initialising an array of char as a string:</p>
<pre class="programlisting">
char message[] = &quot;hello world&quot;;
</pre>
<p>We are not assigning the literal on the right hand side to the
array on the left: the literal in this context is a simply a short
hand for the aggregate initialiser we were using before. Note also
that initialising in this way guarantees you a null terminating
character: it's implied with the double quotes; you get it for
free. Occasionally you see people write rubbish like:</p>
<pre class="programlisting">
char message[] = &quot;hello world\0&quot;;
</pre>
<p>Apparently this is done &quot;just to be safe&quot;. This is nonsense, and
confers no more safety on the code than putting a St Christopher's
in the compiler's original packaging box. Although there are
certainly uses for double null terminating a string, the code above
advertises a lack of C knowledge rather than a clever data
structure.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e84" id="d0e84"></a>Next time</h2>
</div>
<p>There are many topics to cover: arrays versus pointers, string
lifetime, const-ness, comparison, manipulation, dynamic allocation,
and a number of popular string myths, to name but a few. I won't
commit myself just yet as to what the next article will be on: I
will await any comments, questions or requests you might have
(<tt class="literal">kevlin@two-sdg.demon.co.uk</tt>). In the
absence of any feedback, whim will dictate the content of the next
&quot;String Theory&quot;.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
