与指针为const char C的误差[] [英] C error with pointer and const char[]

查看:139
本文介绍了与指针为const char C的误差[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个const char ARR []参数,我想遍历,

I have a const char arr[] parameter that I am trying to iterate over,

char *ptr;
for (ptr= arr; *ptr!= '\0'; ptr++) 
  /* some code*/

我得到一个错误:赋值丢弃了指针目标类型的限定

I get an error: assignment discards qualifiers from pointer target type

现为const char []的处理不同于非const?

Are const char [] handled differently than non-const?

推荐答案

开关* PTR的声明是

Switch the declaration of *ptr to be.

const char* ptr;

问题是,你基本上是指定一个const char *为char *。这是常量的侵犯,因为你从一个const去一个非const。

The problem is you are essentially assigning a const char* to a char*. This is a violation of const since you're going from a const to a non-const.

这篇关于与指针为const char C的误差[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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