如何空指针运算在GCC发生 [英] How void pointer arithmetic is happening in GCC
问题描述
int main()
{
int a;
void *p;
p = &a;
printf("%ld\n",(long)p);
p = p+1;
printf("%ld\n",(long)p);
}
在这个程序中, P + 1
1。只是递增p值,我知道无效指针运算
不是ç可能
,所以 GCC
被隐式这样做。如果是,则它以它为字符指针
。另外,为什么取消引用
是不可能的空指针,如果是隐式做指针运算。
In this program, p+1
is just incrementing the value of p by 1. I know void pointer arithmetic
is not possible in C
, so GCC
is doing it implicitly. And if yes, then is it taking it as char pointer
. Also, why dereferencing
is not possible for void pointer, if it is implicitly doing pointer arithmetic.
推荐答案
C不会允许与指针运算无效*
指针类型。
C does not allow pointer arithmetic with void *
pointer type.
GNU C允许它通过考虑无效
的大小为 1
。
GNU C allows it by considering the size of void
is 1
.
从的 6.23算术上无空隙和函数指针的:
在GNU C,加法和减法运算支持指针为void和函数指针。这是通过处理空隙的函数或大小作为1进行。
In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.
http://gcc.gnu.org/onlinedocs/gcc/Pointer- Arith.html
现在来回答这个问题:
此外,为什么非关联化是不可能的空指针,如果是隐式做指针运算。
Also, why dereferencing is not possible for void pointer, if it is implicitly doing pointer arithmetic.
GNU C允许指针算法与无效*
,但仍不允许类型的对象无效
来声明。
GNU C allows pointer arithmetic with void *
but still does not allow an object of type void
to be declared.
这篇关于如何空指针运算在GCC发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!