Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like everyone else has pointed out, the problem is that <code>dest</code> hasn't been initialized and is pointing to a random location that contains something important. You have several choices of how to deal with this:</p> <ol> <li>Allocate the <code>dest</code> buffer dynamically and return that pointer value, which the caller is responsible for freeing;</li> <li>Assign <code>dest</code> to point to <code>src</code> and modify the value in place (in which case you'll have to change the declaration of <code>a</code> in main() from <code>char *a = "hello world";</code> to <code>char a[] = "hello world";</code>, otherwise you'll be trying to modify the contents of a string literal, which is undefined);</li> <li>Pass the destination buffer as a separate argument.</li> </ol> <p>Option 1 -- allocate the target buffer dynamically:</p> <pre><code>char *to_upper(char *src) { char *dest = malloc(strlen(src) + 1); ... } </code></pre> <p>Option 2 -- have dest point to src and modify the string in place:</p> <pre><code>int main(void) { char a[] = "hello world"; ... } char *to_upper(char *src) { char *dest = src; ... } </code></pre> <p>Option 3 -- have main() pass the target buffer as an argument:</p> <pre><code>int main(void) { char *a = "hello world"; char *b = malloc(strlen(a) + 1); // or char b[12]; ... printf("Uppercase becomes %s\n", to_upper(a,b)); ... free(b); // omit if b is statically allocated return 0; } char *to_upper(char *src, char *dest) { ... return dest; } </code></pre> <p>Of the three, I prefer the third option; you're not modifying the input (so it doesn't matter whether a is an array of char or a pointer to a string literal) and you're not splitting memory management responsibilities between functions (i.e., main() is solely responsible for allocating and freeing the destination buffer). </p> <p>I realize you're trying to familiarize yourself with how pointers work and some other low-level details, but bear in mind that <code>a[i]</code> is easier to read and follow than <code>*(a+i)</code>. Also, there are number of functions in the standard library such as <code>islower()</code> and <code>toupper()</code> that don't rely on specific encodings (such as ASCII):</p> <pre><code>#include &lt;ctype.h&gt; ... if (islower(src[i]) dest[i] = toupper(src[i]); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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