如何摆脱警告:从字符串常量德precated转换为char * [英] How get rid of warning: deprecated conversion from string constant to char*

查看:257
本文介绍了如何摆脱警告:从字符串常量德precated转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何摆脱下面的警告。

 警告:从字符串常量为char * [-Wwrite弦]德precated转换

下面是code。

 #包括LT&;&stdio.h中GT;
#定义MY_STRINGdrivers.txt无效printMyString(字符STR []){
    的printf(STR);
}
诠释主(){
    printMyString(MY_STRING);
}


解决方案

我想你是使用C编译程序++。

在C ++中,字符串文字具有类型为const char [] ,不一样的的char [] 用C

和最好不要为字符串传递给的printf 的格式说明,可能会造成潜在的安全问题。

将其更改为:

 无效printMyString(为const char *海峡){
    的printf(%S,STR);
}

How to get rid of the below warnings.

 warning: deprecated conversion from string constant to char* [-Wwrite-strings]

Here is code.

#include<stdio.h>
#define MY_STRING       "drivers.txt"   

void printMyString(char str[]){
    printf(str);  
}


int main(){
    printMyString(MY_STRING);   
}

解决方案

I think you are compiling the program in C++.

In C++, string literals have type const char[], not the same as char [] in C.

And it's better not to pass strings to printf as format specifier, it may cause potential security problems.

Change it to:

void printMyString(const char *str){
    printf("%s", str);  
}

这篇关于如何摆脱警告:从字符串常量德precated转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆