Reputation: 173
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);
}
Upvotes: 1
Views: 2348
Reputation: 3340
It's a problem related to how const variables are implemented for PIC controller. In PIC controllers the RAM and Code are located in different types of memory. RAM is SD-RAM, and code is flash memory.(RAM is accessible through register and ram operations, and code is only automatically decoded, or read through a complicated sequence of assembly operations.) Since the RAM is very small, const values are stored as Return Literal instructions in the Code Memory(To bypass the difficulty of reading Flash memory otherwise), which is larger.
So you can't really do the same operations on both types. It's something you need to learn to live with. I suggest you look at the disassembly of this code to see what's really going on.
Upvotes: 3
Reputation: 3672
I've seen this before in other compilers a long time ago - note I've never used this compiler before.
The "array is-a pointer" concept of C causes a lot of problems with newbies. To prevent that, some compiler writers got very pernickety (it's a word! Look it up!) and required you to pass the address of a character to a const char *
instead of merely an array.
Can you try CopyString2(buf, &test[0], 3);
?
Or even CopyString2(buf, (const char *)&test[0], 3);
?
Upvotes: 2