这是有效的C语句吗? [英] Is this valid C statement?

查看:88
本文介绍了这是有效的C语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

根据标准,这是有效的:


char TmpPtrWriteBuffer [1000];

void * PtrWriteBuffer =(void *)TmpPtrWriteBuffer;


我和我的同事们辩论说,任何事情都无法解释为

void *虽然反过来是真的。但他们说这是有效的。

我不能同意他们的有效解释。任何C

标准专家都能澄清这一点吗?


问候,

s.subbarayan

Dear all,
According to standards is this valid:

char TmpPtrWriteBuffer[1000];
void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer;

I had a debate with my colleagues that anything cant be typecasted to
void* though the reverse is true.But they said this is valid.
I just can''t agree with them with out a valid explaination.Can any C
standard experts clarify me this?

Regards,
s.subbarayan

推荐答案

s.subbarayan< s _ *** *******@rediffmail.com>潦草地写道:
s.subbarayan <s_**********@rediffmail.com> scribbled the following:
亲爱的,
根据标准,这是有效的:
char TmpPtrWriteBuffer [1000];
void * PtrWriteBuffer =(void *)TmpPtrWriteBuffer;
我和我的同事们辩论说,任何事情都不能说是无效*虽然反过来也是如此。但是他们说这是有效的。
我不能同意他们的意见可以解释一下有效的解释。任何C标准专家都能澄清一下这个吗?
Dear all,
According to standards is this valid: char TmpPtrWriteBuffer[1000];
void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to
void* though the reverse is true.But they said this is valid.
I just can''t agree with them with out a valid explaination.Can any C
standard experts clarify me this?




你是对的。指向任何东西的指针都可以分配到

void *,即使没有强制转换也是如此。但是,这并不意味着任何void *

总能安全地分配到任何其他指针类型中。可能

是对齐冲突或其他可能依赖于实现的问题

问题。

例如,以下代码通常不起作用:


char * cp;

int * ip;

void * vp;

if(cp = malloc(1000)){

vp = cp + 1;

ip = vp;

}


即使代码编译得很好,也可能在运行时导致错误,因为行ip = vp;将指向不正确的指针值分配给int指针。 (假设sizeof(int)> 1。)


-

/ - Joona Palaste(pa ***** @ cc。 helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste --------------- ------规则! -------- /

"犯错是人。要真正搞砸了一台电脑。

- Anon



You are right on this one. A pointer to anything can be assigned into a
void *, even without a cast. However, this does not mean that any void *
can always safely be assigned into any other pointer type. There might
be alignment conflicts or other, possibly implementation-dependent,
issues.
For example the following code will usually not work:

char *cp;
int *ip;
void *vp;
if (cp=malloc(1000)) {
vp = cp+1;
ip = vp;
}

Even though the code compiles nicely, it might cause errors at run-time,
because the line ip = vp; assigns a pointer value that is not properly
aligned to int into a int pointer. (This is assuming sizeof(int)>1.)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon


" s.subbarayan" < S _ ********** @ rediffmail.com>在留言中写道

news:c3 ************************** @ posting.google.c om ...
"s.subbarayan" <s_**********@rediffmail.com> wrote in message
news:c3**************************@posting.google.c om...
亲爱的,
根据标准,这是有效的:

char TmpPtrWriteBuffer [1000];
void * PtrWriteBuffer =(void *)TmpPtrWriteBuffer;

我与同事们辩论说,任何事情都不能说是无效*虽然反过来也是如此。但是他们说这是有效的。
我就是不能如果有任何C
标准专家对此进行澄清,请与他们达成一致。
Dear all,
According to standards is this valid:

char TmpPtrWriteBuffer[1000];
void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer;

I had a debate with my colleagues that anything cant be typecasted to
void* though the reverse is true.But they said this is valid.
I just can''t agree with them with out a valid explaination.Can any C
standard experts clarify me this?




你的同事是对的。类型(void *)

的指针具有相同的对齐要求和内部表示

作为(char *)。符号TmpPtrWriteBuffer恶化

键入(char *),因此将其转换为(void *)并不会损害指针值的完整性。目标符号

" PtrWriteBuffer"有类型(void *),所以从(void *)到(void *)的赋值

也是有效的。



Your colleagues are correct. A pointer of type (void*)
has the same alignment requirements and internal representation
as a (char*). The symbol "TmpPtrWriteBuffer" deteriorates
to type (char*), so casting it to (void*) doesn''t harm
the integrity of the pointer value. The target symbol
"PtrWriteBuffer" has type (void*), so the assignment
from (void*) to (void*) is also valid.


Joona I Palaste < PA ***** @ cc.helsinki.fi>潦草地写道:
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
s.subbarayan< s _ ********** @ rediffmail.com>潦草地写道:
s.subbarayan <s_**********@rediffmail.com> scribbled the following:
亲爱的,
根据标准,这是有效的:
char TmpPtrWriteBuffer [1000];
void * PtrWriteBuffer =(void *)TmpPtrWriteBuffer;
我和我的同事们辩论说,任何事情都不能说是无效*虽然反过来也是如此。但是他们说这是有效的。
我不能同意他们的意见有效的解释。任何C
标准专家都能澄清这一点吗?
Dear all,
According to standards is this valid: char TmpPtrWriteBuffer[1000];
void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to
void* though the reverse is true.But they said this is valid.
I just can''t agree with them with out a valid explaination.Can any C
standard experts clarify me this?


你是对的。指向任何东西的指针都可以分配到
void *中,即使没有强制转换也是如此。但是,这并不意味着任何void *
始终可以安全地分配给任何其他指针类型。可能存在对齐冲突或其他可能依赖于实现的问题。
例如,以下代码通常不起作用:
char * cp;
int * ip;
void * vp;
if(cp = malloc(1000)){
vp = cp + 1;
ip = vp;
}
即使代码编译得很好,也可能在运行时导致错误,因为行ip = vp;将未正确对齐int的指针值分配给int指针。 (假设sizeof(int)> 1。)

You are right on this one. A pointer to anything can be assigned into a
void *, even without a cast. However, this does not mean that any void *
can always safely be assigned into any other pointer type. There might
be alignment conflicts or other, possibly implementation-dependent,
issues.
For example the following code will usually not work: char *cp;
int *ip;
void *vp;
if (cp=malloc(1000)) {
vp = cp+1;
ip = vp;
} Even though the code compiles nicely, it might cause errors at run-time,
because the line ip = vp; assigns a pointer value that is not properly
aligned to int into a int pointer. (This is assuming sizeof(int)>1.)




对不起,在阅读了xarax的回复之后,我发现我已经

回答了错误的问题。是的,你的同事是正确的

a指向任何东西的指针都可以分配给void *有或没有演员。

正如我上面所示,分配回到其他方向将

不一定有效。


-

/ - Joona Palaste(pa ***** @ cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ------------- --------规则! -------- /

当一个男人对一个女人说脏话时,那就是性骚扰。当一个女人对男人说脏话时,那是'每分钟14.99美元+本地电话费!'

- Ruben Stiller



Sorry, after having read xarax''s reply, I have found out that I have
answered the wrong question. Yes, your colleagues are correct in that
a pointer to anything can be assigned to void * with or without a cast.
As I have shown above, the assignment back in the other direction will
not necessarily work.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that''s sexual harassment. When a woman talks
dirty to a man, that''s 14.99 per minute + local telephone charges!"
- Ruben Stiller


这篇关于这是有效的C语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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