Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><strong>a) Where I was wrong?where my programming logic getting fail?</strong></p> </blockquote> <p>Well, you did several things incorrectly. For one, <code>void main</code> is non-standard; the return type for <code>main</code> should be <code>int</code>.</p> <p>That being said, the <em>real</em> issue you're looking for has to do with the fact that <code>q</code> is uninitialized, yet you attempt to still copy to memory through it (which is undefined behavior). To correct that, try allocating <code>q</code>, e.g.</p> <pre><code>char *q = malloc(8); </code></pre> <p>Note you must then later also take care to <code>free</code> the memory allocated here.</p> <p>Aside from that, you're forgetting to copy the <code>NUL</code> terminator, too.</p> <pre><code>*q = 0; </code></pre> <p>... after your copying loop. You also are printing <code>q</code> after incrementing the pointer, so it will no longer be at the head of the string by the time of your <code>printf</code> call. You should store a copy of the head in another variable. Additionally, be careful using a plain <code>printf</code> without any new-lines, as the stream may be buffered and thus could remain unflushed -- use an explicit <code>fflush(stdout);</code> </p> <hr> <blockquote> <p><strong>b) How can I improve this code to get desired output?</strong></p> </blockquote> <p>Well, the first most straight-forward improvement I can think of is to use <a href="http://en.cppreference.com/w/c/string/byte/strcpy" rel="nofollow"><code>strcpy</code></a>.</p> <pre><code>#include &lt;stdio.h&gt; main() { const char p[] = "krishna"; char q[sizeof p]; strcpy(q, p); puts(q); getchar(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload