void **指针打破对称? [英] The void** pointer breaking symmetry?

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

问题描述

嗨Clers,


如果我看一下我过去15年b
年编程的~200000行C代码,有一件烦人的事情在于这种智能语言,它以某种方式减少了源代码的美感;-):


char * cp;

void * vp;

void ** vpp;


// 1

cp = vp;


// 2

cp = * vpp;


为什么第一条指令允许,而第二条指令创建

编译器警告/错误?

如果vpp是一个指向void指针的指针,为什么我不允许将vpp的内容赋给

没有丑陋的显式转换的char指针?为什么

是否需要打破对称?是否有任何GCC编译器选项

专门禁用这个对我没有意义的警告?


感谢您的反馈,

Elmar


PS:如果这是一个老问题:谷歌无法搜索

''void **''变得困难找到答案;-)

解决方案

el *** @ cmbi.ru.nl 写道:

如果我查看我在过去15年内编程的~200000行C代码,有一个这种智能语言令人烦恼,它以某种方式减少了源代码的美感;-):

char * cp;
void * vp;
void ** vpp;

// 1
cp = vp;

// 2
cp = * vpp;

为什么第一条指令允许,而第二条指令创建一个
编译器警告/错误?
如果vpp是指向void指针的指针,为什么我不允许ssign
vpp的内容到char指针没有丑陋的显式转换?为什么需要打破对称?是否有任何GCC编译器选项
专门禁用这个对我没有意义的警告?



这看起来与van der Linden的讨论非常相似'' s

专家C编程。标准

中有一个特定部分涉及有关具体约束的任务。我现在没有b $ b或者其他方便,但在这种情况下编译错误感觉

正确。


< blockquote>

el***@cmbi.ru.nl 写道05/05/06 14:44,:

嗨Clers,

如果我看看过去15年来编程的~200000行C代码,有一个令人讨厌这种智能语言的东西,它以某种方式减少了源代码的美感;-):

char * cp;
void * vp;
void ** vpp;

// 1
cp = vp;

// 2
cp = * vpp;

为什么第一条指令允许,而第二条指令创建
编译器警告/错误?


两者都是合法的,假设vp在

第一个实例中具有有效值,而vpp指向具有有效
$ b的void *第二个$ b值。


也许编译器会警告'= *'',

是现在拼写的运算符的古董形式`* = ''。如果

所以,它试图告诉你'= *''不再意味着它在C的早期就做了什么(以防万一)你是编译一些非常古老的代码的b $ b,你可以通过写'= *'来静音

警告。


但是没有任何保证:编译器允许

发出尽可能多的警告,即使对于明确定义的构造

也是如此。大多数人认为这有用

如果(a = b)

printf(Equal \ n ;);

else

printf(" Unequal\\\
); / *不再... * /


....这是一个完全有效的C片段,但也是一个常见的

滑动的手指。

如果vpp是一个指向void指针的指针,为什么我不允许在没有丑陋的显式转换的情况下将vpp的内容分配给char指针?


允许分配,不需要演员。

为什么需要打破对称?是否有任何GCC编译器选项
专门禁用这个对我没有意义的警告?




也许你会告诉我们 ;这个警告而不是每个人都猜测它,有人会有一个想法。


-
Er ********* @ sun.com


很抱歉,我不小心粘贴了两个

对称相关案例的''正确'':


这是完整的代码:


char * cp;

void * vp;

void ** vpp;


/ / 1

cp = vp;

// 2

cp = * vpp;


// 3

vp = cp;

// 4

vpp =& cp;

简而言之:如果1和2工作,为什么返回工作在案例3中但

在案例4中失败,并带有GCC消息警告:分配来自

不兼容的指针类型 ?


再次感谢,

Elmar


Hi Clers,

If I look at my ~200000 lines of C code programmed over the past 15
years, there is one annoying thing in this smart language, which
somehow reduces the ''beauty'' of the source code ;-):

char *cp;
void *vp;
void **vpp;

// 1
cp=vp;

// 2
cp=*vpp;

Why is the first instruction allowed while the second one creates a
compiler warning/error?
If vpp is a pointer to a void pointer, why am I not allowed to assign
the content of vpp to a char pointer without ugly explicit casts? Why
is it needed to break the symmetry? Are there any GCC compiler options
to specifically disable this warning which doesn''t make sense to me?

Thanks for your feedback,
Elmar

P.S.: In case this is an old question: Googles inability to search for
''void**'' made it hard to find the answer ;-)

解决方案

el***@cmbi.ru.nl wrote:

If I look at my ~200000 lines of C code programmed over the past 15
years, there is one annoying thing in this smart language, which
somehow reduces the ''beauty'' of the source code ;-):

char *cp;
void *vp;
void **vpp;

// 1
cp=vp;

// 2
cp=*vpp;

Why is the first instruction allowed while the second one creates a
compiler warning/error?
If vpp is a pointer to a void pointer, why am I not allowed to assign
the content of vpp to a char pointer without ugly explicit casts? Why
is it needed to break the symmetry? Are there any GCC compiler options
to specifically disable this warning which doesn''t make sense to me?


This looks surprisingly similar to a discussion in van der Linden''s
"Expert C Programming". There is a particular part in the Standard
regarding assignments that talks about specific constraints. I don''t
have either handy right now, but the compiler error in this case feels
right.




el***@cmbi.ru.nl wrote On 05/05/06 14:44,:

Hi Clers,

If I look at my ~200000 lines of C code programmed over the past 15
years, there is one annoying thing in this smart language, which
somehow reduces the ''beauty'' of the source code ;-):

char *cp;
void *vp;
void **vpp;

// 1
cp=vp;

// 2
cp=*vpp;

Why is the first instruction allowed while the second one creates a
compiler warning/error?
Both are legal, assuming vp has a valid value in the
first instance and vpp points to a void* with a valid
value in the second.

Perhaps the compiler is warning about the `=*'', which
was an antique form of the operator now spelled `*=''. If
so, it''s trying to tell you that `=*'' no longer means what
it did in the very early days of C (just in case you''re
compiling some very old code), and you can probably silence
the warning by writing `= *'' instead.

There are no guarantees, though: The compiler is allowed
to issue as many warnings as it wants, even for constructs
that are well-defined. Most people consider this helpful
in cases like

if (a = b)
printf ("Equal\n");
else
printf ("Unequal\n"); /* Not any more ... */

.... which is a perfectly valid C fragment, but also a common
slip of the finger.
If vpp is a pointer to a void pointer, why am I not allowed to assign
the content of vpp to a char pointer without ugly explicit casts?
The assignment is allowed, and no cast is required.
Why
is it needed to break the symmetry? Are there any GCC compiler options
to specifically disable this warning which doesn''t make sense to me?



Perhaps if you''d show us "this warning" instead of making
everybody guess about it, someone would have an idea.

--
Er*********@sun.com


Big apologies, I accidentally pasted only the ''correct'' of the two
symmetry related cases:

Here is the complete code:

char *cp;
void *vp;
void **vpp;

// 1
cp=vp;
// 2
cp=*vpp;

// 3
vp=cp;
// 4
vpp=&cp;
In short: if 1 and 2 work, why does the reversion work in case 3 but
fail in case 4 with the GCC message "warning: assignment from
incompatible pointer type" ?

Thanks again,
Elmar


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

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