德$ P $从字符串常量在C pcated转换“的char *' [英] deprecated conversion from string constant to 'char*' in c

查看:167
本文介绍了德$ P $从字符串常量在C pcated转换“的char *'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理字符串。

每当我执行下面的程序,我得到一个错误的从字符串常量'字符的'在C * 就行了<$ C德precated转换$ C>的char * p =你好

我究竟做错了什么?

这是什么意思的错误?如何改正?

我的code是:

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    的char * p =你好;
    的printf(%S,P + 1);
    返回0;
}


解决方案

这应该是一个警告(虽然你可能设置你的编译器把警告作为错误,这往往是一个好主意)。

你想要的是:字符常量* P =你好; 而不是

试图修改字符串给出未定义的行为。在常量 prevents你这样做,意外(即,code,试图通过写指针将无法编译,除非你删除常量预选赛中,如用铸)。

I am working with strings .

Whenever I execute the following program I get an error as deprecated conversion from string constant to 'char' in c* on the line char *p = "hello"

What am i doing wrong?

What does this error mean ?How can i correct it?

My code is:

#include<stdio.h>
int main()
{
    char *p = "hello";
    printf("%s",p+1);
    return 0;
}

解决方案

This should be a warning (though you may have set your compiler to treat warnings as errors, which is often a good idea).

What you want is: char const *p = "hello"; instead.

Attempting to modify a string literal gives undefined behavior. The const prevents you from doing that by accident (i.e., code that attempts to write via the pointer won't compile, unless you remove the const qualifier, such as with a cast).

这篇关于德$ P $从字符串常量在C pcated转换“的char *'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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