Pass char* to const char* in c
<p>I got a problem. I want to use one <code>CopyString()</code> function to copy <code>const char*</code>s and <code>char*</code>s to a buffer. It shouldn't be a problem but for some reason my compiler doesn't do the thing I expect it to do (I use MikroC for PIC pro).</p>

<pre><code> void CopyString(char * dest, const char * src, uint8 maxLength){
    uint8 i;
    for(i=0 ; src[i] &amp;&amp; ( i &lt; maxLength ) ; i++){
        dest[i] = src[i];
    };
}   

char test[3]={'2', '0', ' '};
CopyString(buf, test, 3);//gives an error (illigal pointer conversion)
CopyString(buf, (const char *)test, 3); //Doesn't pass the pointer to the array... No idea why not... Do you?

void CopyString2(char * dest, char * src, uint8 maxLength){
    uint8 i;
    for(i=0 ; src[i] &amp;&amp; ( i &lt; maxLength ) ; i++){
        dest[i] = src[i];
    };
}   
const char test[3]={'2', '0', ' '};
CopyString2(buf, "20 ", 3);//All ok
CopyString2(buf, test, 3); //gives an error (illigal pointer conversion)
</code></pre>

<p>Anyone knows how to do this? Currently I use <code>CopyString2()</code> and <code>CopyString()</code> in one C document which isn't looking nice and it shouldn't be required. Why would pass a <code>char*</code> to a <code>const char*</code> give problems? The only thing <code>const char*</code> does is making sure the data in the char array won't change within the function, right?</p>

<p>Edit:
Here an edit with the 'whole' example code (which should be a  Minimal, Complete, and Verifiable example).
With Keil compiler (which I use to program for ARM mcu's) it compiles just fine (like I would expect) but with MikroC PIC compiler (as far as I can find this is how the compiler is called.) it gives the following error:</p>

<blockquote>
  <p>18 384 Illegal pointer conversion main.c
  (line 18, message no. 384)</p>
  
  <p>23 384 Illegal pointer conversion main.c
  (line 23, message no. 384)</p>
</blockquote>

<pre><code>void CopyString(char * dest, const char * src, unsigned char maxLength){
    unsigned char i;
    for(i=0 ; src[i] &amp;&amp; ( i &lt; maxLength ) ; i++){
        dest[i] = src[i];
    };
}   

void CopyString2(char * dest, char * src, unsigned char maxLength){
    unsigned char i;
    for(i=0 ; src[i] &amp;&amp; ( i &lt; maxLength ) ; i++){
        dest[i] = src[i];
    };
}   

void main(){
    char buf[16]={0,};
    char test1[3]={'2', '0', ' '};
    const char test2[3]={'2', '0', ' '};

    CopyString(buf, test1, 3);//gives an error (illigal pointer conversion);
    CopyString(buf, (const char *)test1, 3); //Doesn't pass the pointer to the array

    CopyString2(buf, "20 ", 3);//All ok
    CopyString2(buf, test2, 3); //gives an error (illigal pointer conversion);
}
</code></pre>