return从指针目标类型中丢弃限定符 [英] return discards qualifiers from pointer target type

查看:61
本文介绍了return从指针目标类型中丢弃限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i Group,


我理解,将函数参数定义为const意味着

该函数不会改变它。


为什么编译器会说从指针返回丢弃限定词

目标类型当我*访问*定义为const的参数的成员?


请参阅下面的代码:


/ *** START TEST .C *** /

#include< stdio.h>

#include< string.h>

#include< stdlib.h>


#define MAX_NAME_SIZE 80

#define MY_NAME"这是我的名字"


struct my_s

{

char mys_name [MAX_NAME_SIZE + 1];

};


void set_mys_name(struct my_s *,const char *);

char * get_mys_name(const struct my_s *);


int main(void){

struct my_s * mysp;


mysp = malloc(sizeof * mysp);


set_mys_name(mysp,MY_NAME);


printf(我的名字是%s \ n,get_mys_name(mysp));


return(0); < br $>
}


void

set_mys_name(struct my_s * mys,const char * name)

{

如果(!mys)返回;

strncpy(mys- > mys_name,name,MAX_NAME_SIZE);

}


char *

get_mys_name(const struct my_s * mys)

{

if(!mys)return(NULL);

return(mys-> mys_name);

}

/ *** START TEST.C *** /

i Group,

to my understanding, defining a function parameter as "const" means that
the function is not going to change it.

Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?

Please see the code below:

/*** START TEST.C ***/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_NAME_SIZE 80
#define MY_NAME "This is my name"

struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};

void set_mys_name(struct my_s *, const char *);
char *get_mys_name(const struct my_s *);

int main(void) {
struct my_s *mysp;

mysp = malloc(sizeof *mysp);

set_mys_name(mysp, MY_NAME);

printf("My name is %s\n", get_mys_name(mysp));

return (0);
}

void
set_mys_name(struct my_s *mys, const char *name)
{
if(!mys) return;
strncpy(mys->mys_name, name, MAX_NAME_SIZE);
}

char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
/*** START TEST.C ***/


gcc -Wall -o test test.c
gcc -Wall -o test test.c



test.c:在函数`get_mys_name''中:

test.c:39:警告:return从指针目标类型中丢弃限定符

test.c: In function `get_mys_name'':
test.c:39: warning: return discards qualifiers from pointer target type


./test
./test



我的名字是这是我的名字

谢谢!

-

Pietro Cerutti

PGP公钥:
http://gahr.ch/pgp

推荐答案

Pietro Cerutti< ga ** @ gahr.chwrites:
Pietro Cerutti <ga**@gahr.chwrites:

i Group,


据我所知,将函数参数定义为const意味着

该函数不会改变它。


为什么编译器会说从指针返回丢弃限定词

目标类型当我*访问*定义为const的参数的成员?
i Group,

to my understanding, defining a function parameter as "const" means that
the function is not going to change it.

Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?



< snip>

<snip>


struct my_s

{

char mys_name [MAX_NAME_SIZE + 1];

};
struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};



....

....


char *

get_mys_name(const struct my_s * mys)

{

if(!mys)return(NULL);

return(mys-> mys_name);

}
char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}



mys指向的对象是const。这个函数返回一个普通的

(非常量)指针到它的一部分(mys_name数组)。调用者

可以通过此ponter修改此数组的内容。

编译器警告您此函数会破坏其参数指向的

对象的常量。


-

Ben。

The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.

--
Ben.


Ben Bacarisse写道:
Ben Bacarisse wrote:

Pietro Cerutti< ga * *@gahr.chwrites:
Pietro Cerutti <ga**@gahr.chwrites:

> i Group,

根据我的理解,将函数参数定义为const。意味着该功能不会改变它。

为什么编译器会说从指针返回丢弃限定符
目标类型。当我*访问*定义为const的参数的成员?
>i Group,

to my understanding, defining a function parameter as "const" means that
the function is not going to change it.

Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?



< snip>


<snip>


> struct my_s
{char mys_name [MAX_NAME_SIZE + 1];
};
>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};



...

...


> char *
get_mys_name(const struct my_s * mys )
{
if(!mys)return(NULL);
return(mys-> mys_name);
}
>char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}



mys指向的对象是const。这个函数返回一个普通的

(非常量)指针到它的一部分(mys_name数组)。调用者

可以通过此ponter修改此数组的内容。

编译器警告您此函数会破坏其参数指向的

对象的常量。


The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.



所以,可以这么说,当一个struct定义为const时,每个指针尝试使用
来引用该struct的成员 - >操作员必须

也是const。

我是否正确?


感谢您的投入!

-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp


Ben Bacarisse写道:
Ben Bacarisse wrote:

Pietro Cerutti< ga ** @ gahr.chwrites:
Pietro Cerutti <ga**@gahr.chwrites:

> i Group,
据我所知,将函数参数定义为const。意味着该功能不会改变它。

为什么编译器会说从指针返回丢弃限定符
目标类型。当我*访问*定义为const的参数的成员?
>i Group,

to my understanding, defining a function parameter as "const" means that
the function is not going to change it.

Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?



< snip>


<snip>


> struct my_s
{char mys_name [MAX_NAME_SIZE + 1];
};
>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};



...

...


> char *
get_mys_name(const struct my_s * mys )
{
if(!mys)return(NULL);
return(mys-> mys_name);
}
>char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}



mys指向的对象是const。这个函数返回一个普通的

(非常量)指针到它的一部分(mys_name数组)。调用者

可以通过此ponter修改此数组的内容。

编译器警告您此函数会破坏其参数指向的

对象的常量。


The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.



很抱歉双重发布:

我虽然参数中的const只表示函数本身

不会修改参数。对象本身(结构)

还没有被定义为const,所以调用get_mys_name()的函数有

使用返回的指针修改名称的权限通过功能。

它有什么问题?

-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp


这篇关于return从指针目标类型中丢弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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