I got a problem. I want to use one CopyString() function to copy const char*s and char*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).
void CopyString(char * dest, const char * src, uint8 maxLength){
uint8 i;
for(i=0 ; src[i] && ( i < 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] && ( i < 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)
Anyone knows how to do this? Currently I use CopyString2() and CopyString() in one C document which isn't looking nice and it shouldn't be required. Why would pass a char* to a const char* give problems? The only thing const char* does is making sure the data in the char array won't change within the function, right?
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:
18 384 Illegal pointer conversion main.c (line 18, message no. 384)
23 384 Illegal pointer conversion main.c (line 23, message no. 384)
void CopyString(char * dest, const char * src, unsigned char maxLength){
unsigned char i;
for(i=0 ; src[i] && ( i < maxLength ) ; i++){
dest[i] = src[i];
};
}
void CopyString2(char * dest, char * src, unsigned char maxLength){
unsigned char i;
for(i=0 ; src[i] && ( i < 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);
}
bufdeclared ? Please provide a Minimal, Complete, and Verifiable example, otherwise the question is likely downvoted or closed. - Jabberwocky