相关疑难解决方法(0)

为什么我得到; 使用'const char*'类型的表达式初始化'char*'会丢弃限定符?

我无法弄清楚为什么我自己得到这个警告clang:

function_prototype_const_modifier.c:13:8: warning: initializing 'char *' with an
      expression of type 'const char *' discards qualifiers
      [-Wincompatible-pointer-types]
        char *ptr1 = source;
              ^      ~~~~~~
1 warning generated.
Run Code Online (Sandbox Code Playgroud)

代码很简单

#include<stdio.h>

char *my_strcpy(char *destination, const char *source);

int main(void) {
    char str1[] = "this is something";  
    char str2[] = "123456789123456789";
    my_strcpy(str2, str1);
    puts(str2);
    return 0;
}
char *my_strcpy(char *destination, const char *source) {
    char *ptr1 = source;
    char *ptr2 = destination;
    while(*ptr1 != '\0') {
        *ptr2++ = *ptr1++;
    }
    *ptr2 = …
Run Code Online (Sandbox Code Playgroud)

c

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×1