简体   繁体   English

将char *传递给c中的const char *

[英]Pass char* to const char* in c

I got a problem. 我有问题 I want to use one CopyString() function to copy const char* s and char* s to a buffer. 我想使用一个CopyString()函数将const char*char*复制到缓冲区。 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). 这应该不成问题,但是由于某种原因,我的编译器没有执行我期望的操作(我将MikroC用于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. 目前,我在一个C文档中使用CopyString2()CopyString()看起来不太好,因此不需要。 Why would pass a char* to a const char* give problems? 为什么将char*传递给const char*带来问题? The only thing const char* does is making sure the data in the char array won't change within the function, right? const char*唯一要做的就是确保char数组中的数据在函数内不会改变,对吗?

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: 使用Keil编译器(我用来为ARM mcu编程)时,它可以很好地编译(就像我期望的那样),但是使用MikroC PIC编译器(据我所知,这就是编译器的调用方式。),它将产生以下错误:

18 384 Illegal pointer conversion main.c (line 18, message no. 384) 18384非法的指针转换main.c(第18行,消息号384)

23 384 Illegal pointer conversion main.c (line 23, message no. 384) 23384非法的指针转换main.c(第23行,消息号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);
}

It's a problem related to how const variables are implemented for PIC controller. 这是与如何为PIC控制器实现const变量有关的问题。 In PIC controllers the RAM and Code are located in different types of memory. 在PIC控制器中,RAM和代码位于不同类型的存储器中。 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. RAM是SD-RAM,代码是闪存。(可通过寄存器和ram操作访问RAM,并且代码只能自动解码,或通过复杂的汇编操作序列读取。)由于RAM很小,因此const值是作为Return Literal指令存储在代码存储器中(以避开否则读取Flash存储器的难度),该值较大。

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. 我建议您看一下这段代码的反汇编,以了解实际情况。

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. C的“数组是指针”概念引起新手的很多问题。 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. 为避免这种情况,一些编译器编写者感到非常恼火(这是一个词!查找它!),并要求您将字符的地址传递给const char *而不是数组。

Can you try CopyString2(buf, &test[0], 3); 你可以尝试CopyString2(buf, &test[0], 3); ?

Or even CopyString2(buf, (const char *)&test[0], 3); 甚至是CopyString2(buf, (const char *)&test[0], 3); ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM