常型问题 [英] const type question

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

问题描述

int intcmp(const void * a,const void * b)

{

return(*(int *)a - *(int *)b);

}

上面的
,如果我把''void''代替''const void''作为

参数,

有什么区别?


i在参数中使用时不能得到const的含义..


提前致谢〜

int intcmp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}

in the above , if i put just ''void'' instead of ''const void'' as a
parameter,

what''s the difference ?

i can''t get the meaning of const when used in parameter..

Thanks in advance ~

推荐答案

" herrcho" <他********* @ kornet.net>写道:
"herrcho" <he*********@kornet.net> wrote:
int intcmp(const void * a,const void * b)
{
return(*(int *)a - *(int *) b);
}

在上面,如果我把''void''而不是''const void''作为
参数,

有什么区别?


没什么,因为你无论如何都不想改变任何东西。

i在参数中使用时不能得到const的含义..
int intcmp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}

in the above , if i put just ''void'' instead of ''const void'' as a
parameter,

what''s the difference ?
Nothing, since you''re not trying to change anything anyway.
i can''t get the meaning of const when used in parameter..




如果参数声明为const,则该函数无法更改它。这个

听起来没有变化,因为函数无论如何都无法改变调用者中参数的值

。但是,如果参数是const,

函数甚至不能在函数_中更改其值。

如果参数是指向const类型的指针,则函数无法更改

指针所指向的对象。


如果你没有修改函数中的任何内容,那么这不是

当然是重要的。但是,上面看起来像qsort()(或bsearch())的比较函数

。在这种情况下,consts会在那里使用
阻止你更改qsort()后面的对象,因为这样做可能会使排序算法陷入困境。


Richard



If a parameter is declared const, the function cannot change it. This
sounds like it''s no change, since a function cannot change a the value
of a parameter in the caller anyway. However, if the parameter is const,
the function cannot even change its value _within the function_.
If a parameter is a pointer to a const type, the function cannot change
the object the pointer points at.

If you don''t modify anything in the function to begin with, this doesn''t
matter, of course. However, the above looks like a comparison function
for qsort() (or bsearch()). In that case, the consts are there to
prevent you from changing the objects behind qsort()''s back, since doing
so could mess up the sorting algorithm.

Richard


" herrcho" <他********* @ kornet.net>写道:
"herrcho" <he*********@kornet.net> wrote:
int intcmp(const void * a,const void * b)
{
return(*(int *)a - *(int *)b); <在上面,如果我把''void''而不是''const void''作为
参数,

什么有区别吗?


如果您打算将此函数传递给qsort或其他什么需要参数类型为int(*)的
(const void *,const void *)

那么你不能从const void更改为void,这将是

a约束违规(错误的参数类型)或者未定义

行为(如果你说把它强制转换为正确的类型)。

i在参数中使用时不能得到const的含义..
int intcmp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}

in the above , if i put just ''void'' instead of ''const void'' as a
parameter,

what''s the difference ?
If you''re intending to pass this function to qsort or something
that expects a parameter of type int(*)(const void*,const void*)
then you must not change from const void to void, this will be
a constraint violation (wrong type of argument) or else undefined
behaviour (if you say cast it back to the right type).
i can''t get the meaning of const when used in parameter..



当用作'const类型* a''时,表示不允许你修改指针指向的东西。
。不幸的是

由于你写这个函数的方式不好这个保护丢失

你把参数转换为(int *)。事实上你可以用完全类型安全的方式编写任何

qsort比较函数,而没有

强制转换,这是更清晰的代码:


int intcmp(const void * va,const void * vb)

{

const int * a = va,* b = vb;

返回* a - * b;

}


这样,如果

,您将从编译器获得诊断消息你不小心试图修改内存的内容。


-

Simon。



When used as ''const type *a'' it means that you are not allowed to
modify the thing that the pointer is pointing to. Unfortunately
due to the bad way you wrote the function this protection is lost
when you cast the argument to (int*). You can in fact write any
qsort compare function in a completely typesafe manner with no
casts, which is much cleaner code:

int intcmp(const void *va, const void *vb)
{
const int *a = va, *b = vb;
return *a - *b;
}

That way you will get a diagnostic message from the compiler if
you accidentally attempt to modify the contents of the memory.

--
Simon.


Simon Biber写道:
Simon Biber wrote:

.... snip ...
当用作'const类型* a''时,意味着你不允许
修改指针指向的东西。不幸的是
由于你编写函数的方式不好,当你将参数转换为(int *)时,这种保护就会丢失。实际上你可以用一种完全类型安全的方式编写任何
qsort比较函数而没有
强制转换,这是更清晰的代码:

int intcmp(const void * va,const void * vb)
{
const int * a = va,* b = vb;
返回* a - * b;
}

那样如果您不小心尝试修改内存中的内容,您将收到编译器的诊断消息。
.... snip ...
When used as ''const type *a'' it means that you are not allowed to
modify the thing that the pointer is pointing to. Unfortunately
due to the bad way you wrote the function this protection is lost
when you cast the argument to (int*). You can in fact write any
qsort compare function in a completely typesafe manner with no
casts, which is much cleaner code:

int intcmp(const void *va, const void *vb)
{
const int *a = va, *b = vb;
return *a - *b;
}

That way you will get a diagnostic message from the compiler if
you accidentally attempt to modify the contents of the memory.




完全同意,除了实际比较之外你的例子,

这可能会导致整数溢出问题。这是一个很好的

地点:


返回(* a> * b) - (* a< * b);


-

Chuck F(cb********@yahoo.com)(cb********@worldnet.att。 net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



Totally agree, EXCEPT for the actual comparison in your example,
which is liable to problems from integer overflow. This is a good
place for:

return (*a > *b) - (*a < *b);

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


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

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