常量指针 [英] const pointers

查看:57
本文介绍了常量指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我声明指针如下:


const int * pData; //指向只读数据的指南

int * const pData2; //只读指针


然后我做了以下任务:


pData = pData2;


pData2的只读方面没有任何被这个赋值损坏的危险,而pData只是指向它无法修改的数据。但是我在编译时收到以下错误:


警告:赋值会从指针目标类型中丢弃限定符。


为什么会丢弃限定符?

I declare pointers as follows:

const int* pData; //pointer to read only data
int* const pData2; //read only pointer

Then I make the following assignment:

pData = pData2;

the read-only aspect of pData2 is not in any danger of being
corrupted by this assignment, and pData simply points to data that it
cannot modify. yet I am getting the following error on compilation:

warning: assignment discards qualifiers from pointer target type.

why would qualifiers be discarded?

推荐答案



" LBJ" <磅**** @ yahoo.com>在留言中写道

news:62 ************************ @ posting.google.com ...

"LBJ" <lb****@yahoo.com> wrote in message
news:62************************@posting.google.com ...
我声明指针如下:

const int * pData; //指向只读数据的指针
int * const pData2; //只读指针

然后我做了以下任务:

pData = pData2;

pData2的只读方面不在任何被这个任务破坏的危险,pData只是指向它无法修改的数据。但是我在编译时遇到以下错误:

警告:赋值会从指针目标类型中丢弃限定符。

为什么要丢弃限定符?
I declare pointers as follows:

const int* pData; //pointer to read only data
int* const pData2; //read only pointer

Then I make the following assignment:

pData = pData2;

the read-only aspect of pData2 is not in any danger of being
corrupted by this assignment, and pData simply points to data that it
cannot modify. yet I am getting the following error on compilation:

warning: assignment discards qualifiers from pointer target type.

why would qualifiers be discarded?




因为它们意味着不同的东西。 IIRCconst int * p意味着
指向的是不变的,例如比如const void *例如,许多C函数需要[memcpy

]。


另一方面,int * const p指的是指针本身p。是不变的。


为什么你得到的错误是因为它们不是同一类型所以

编译器可以发出它想要的任何警告。在这种情况下,你会失去这样的事实,即

pdata2是不变的[并且pdata]不是。


Tom



Because they mean different things. IIRC "const int * p" means what p
points to is constant e.g. like "const void *" many C functions take [memcpy
for instance].

On the other hand "int * const p" means the pointer itself "p" is constant.

Why you get the error though is that they''re not the same type so the
compiler can emit any warning it wants. In this case you lose the fact that
pdata2 is constant [and pdata] is not.

Tom

lb****@yahoo.com (LBJ)写道:
lb****@yahoo.com (LBJ) wrote:
我声明指针如下:

const int * pData; //指向只读数据的指针
int * const pData2; //只读指针

然后我做了以下任务:

pData = pData2;

pData2的只读方面不在任何被这个任务破坏的危险,pData只是指向它无法修改的数据。但是我在编译时遇到以下错误:

警告:赋值会从指针目标类型中丢弃限定符。

为什么要丢弃限定符?
I declare pointers as follows:

const int* pData; //pointer to read only data
int* const pData2; //read only pointer

Then I make the following assignment:

pData = pData2;

the read-only aspect of pData2 is not in any danger of being
corrupted by this assignment, and pData simply points to data that it
cannot modify. yet I am getting the following error on compilation:

warning: assignment discards qualifiers from pointer target type.

why would qualifiers be discarded?




奇怪。我可以看到一个限定符被丢弃,但它是指针类型本身的限定符,而不是[我将如何解释这个短语]

指针目标类型。因为你不打算通过更改pData中的值副本来修改原始的

指针对象,所以我看不到

警告的原因。你确定你没有意外地以错误的方式写出

作业(或声明)吗?


Richard



Odd. I can see that a qualifier is discarded, but it''s a qualifier for
the pointer type itself, not for [how I would interpret the phrase] the
pointer target type. And since you''re not going to modify the original
pointer object by changing the copy of its value in pData, I see no
reason for the warning. Are you sure you didn''t accidentally write the
assignment (or the declarations) the wrong way ''round?

Richard


LBJ< lb **** @ yahoo.com>这样说:
LBJ <lb****@yahoo.com> spoke thus:
const int * pData; //指向只读数据的指针
int * const pData2; //只读指针

然后我做了以下任务:
pData = pData2;
警告:赋值从指针目标类型中丢弃限定符。
const int* pData; //pointer to read only data
int* const pData2; //read only pointer

Then I make the following assignment:
pData = pData2; warning: assignment discards qualifiers from pointer target type.



FWIW(并且相信一个人的编译器至少是一个很小的罪恶......) :


#include< stdio.h>


int main(无效)

{

int a = 3;

const int * p;

int * const p2 =& a;

p = p2 ;

返回0;

}


/ usr / bin / gcc -Wall -W -O2 -ansi -pedantic test .c


没有给出任何消息。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



FWIW (and believing one''s compiler is at least a venial sin here...):

#include <stdio.h>

int main( void )
{
int a=3;
const int *p;
int* const p2=&a;
p=p2;
return 0;
}

/usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c

gives no messages whatsoever.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于常量指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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