snipt

Ctrl+h for KB shortcuts

C++

Initialize char array(C-style string) to NULL, and strcat function

char array[SIZE] = {0};

char array[SIZE] = "";

char array[SIZE] = "\0";


//ATTENTIONS about strcat() function

char * strcat ( char * destination, const char * source );

//When use strcat to concatenate strings, the destination string must be initialized, at lease initialized to NULL
https://snipt.net/embed/c3ae3a7d0e2b6908364678689241f8bd/
/raw/c3ae3a7d0e2b6908364678689241f8bd/
c3ae3a7d0e2b6908364678689241f8bd
cpp
C++
12
2019-06-17T04:53:19
True
False
False
/api/public/snipt/43402/
initialize-char-arrayc-style-string-to-null-and-strcat-function
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kt">char</span> <span class="n">array</span><span class="p">[</span><span class="n">SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="kt">char</span> <span class="n">array</span><span class="p">[</span><span class="n">SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="kt">char</span> <span class="n">array</span><span class="p">[</span><span class="n">SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;</span><span class="se">\0</span><span class="s">&quot;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="c1">//ATTENTIONS about strcat() function</span> </span><span id="L-9"><a name="L-9"></a> </span><span id="L-10"><a name="L-10"></a><span class="kt">char</span> <span class="o">*</span> <span class="nf">strcat</span> <span class="p">(</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">destination</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">source</span> <span class="p">);</span> </span><span id="L-11"><a name="L-11"></a> </span><span id="L-12"><a name="L-12"></a><span class="c1">//When use strcat to concatenate strings, the destination string must be initialized, at lease initialized to NULL</span> </span></pre></div> </td></tr></table>
string