关于void指针的问题 [英] Question about void pointers

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

问题描述

这是否有效?


int a [20];

void * b;


b = (void *)a; // b指向[0]


b + = 5 * sizeof(* a); // b指向[5]

a [5] = 100;


printf("%d \ n, *((int *)b)); //打印100


如果是,如果一个结构,它还能运行吗?


是否有可能是阵列可以包含一些填充,所以

而不是sizeof,赋值将是


b + = 5 *((void *)(&(a [1 ])) - (void *)(&(a [0]));


看起来更复杂。


无论如何,任何填充都被纳入sizeof?

Is this valid?

int a[20];
void *b;

b = (void *)a; // b points to a[0]

b += 5*sizeof(*a); // b points to a[5]

a[5] = 100;

printf( "%d\n" , *((int *)b) ); // prints 100

If so, if a had been a struct, would it still work?

Is there a possibility that the array could contain some padding, so
rather than sizeof, the assignment would be

b += 5*( (void *)(&(a[1])) - (void *)(&(a[0]));

which seems more more complex.

Would any padding be incorporated into sizeof anyway?

推荐答案

9月16日下午6:15,raph ... @ gmail.com写道:
On Sep 16, 6:15 pm, raph...@gmail.com wrote:

这是否有效?


int a [20];

void * b;


b =(void *)a; // b指向[0]
Is this valid?

int a[20];
void *b;

b = (void *)a; // b points to a[0]



不需要演员。

No need for the cast.


b + = 5 * sizeof(* a); // b指向a [5]
b += 5*sizeof(*a); // b points to a[5]



否,它无效。你不能在void

*指针上执行算术运算。

如果它编译,那是因为您已启用扩展程序。扩展

是专门用于编译器的新闻组的主题,它使用了



假设你有类似


b =((unsigned char *)b)+ 5 * sizeof * a;


是的,没关系。

No, it''s not valid. you can not perform arithmetic operations on void
* pointers.
If it compiles, it''s because you have extensions enabled. Extensions
are topical to the newsgroup dedicated to the compiler that makes use
of them.
Assuming you had something like

b = ((unsigned char *)b) + 5 * sizeof *a;

Yes, that would be ok.


a [5] = 100;


printf("%d \ n",*((int *)b)); //打印100
a[5] = 100;

printf( "%d\n" , *((int *)b) ); // prints 100



是的,这是有效的。假设你使用我的修复程序。

Yes, that is valid. Assuming you use my fixes.


如果是,如果a是结构,它还能运行吗?
If so, if a had been a struct, would it still work?



是的,但是在< stddef.h>中使用offsetof()宏

Yes, but by using the offsetof() macro in <stddef.h>


是有可能数组可能包含一些填充,所以

而不是sizeof,赋值是
Is there a possibility that the array could contain some padding, so
rather than sizeof, the assignment would be



No.Arrays don'' t有填充字节。

No. Arrays don''t have padding bytes.


b + = 5 *((void *)(&(a [1])) - (void *)(&(a [0]);


这看起来更复杂。


无论如何,任何填充都会被纳入sizeof吗?
b += 5*( (void *)(&(a[1])) - (void *)(&(a[0]));

which seems more more complex.

Would any padding be incorporated into sizeof anyway?



是的,sizeof报告一个对象的大小。填充位和字节属于对象大小的



如果sizeof(unsigned int)== 4和CHAR_BIT == 8,那并不意味着

unsigned int有32个值位。

它可能有16个或更少的填充位。(但不多,因为

保证UINT_MAX> = 65535)

Yes, sizeof reports the size of an object. Padding bits & bytes belong
to the size of the object.

if sizeof (unsigned int) == 4 and CHAR_BIT == 8, it doesn''t mean
unsigned int has 32 value bits.
It might have 16 or less padding bits. (but not more, because of the
guarantee that UINT_MAX >= 65535)


On Se p 16,8:15 * pm,raph ... @ gmail.com写道:
On Sep 16, 8:15*pm, raph...@gmail.com wrote:

这有效吗?


int a [20];

void * b;


b =(void *)a; * // b指向[0]


b + = 5 * sizeof(* a); // b指向[5]

a [5] = 100;


printf("%d \ n, *((int *)b)); //打印100


如果是,如果一个结构,它还能运行吗?


是否有可能是阵列可以包含一些填充,所以

而不是sizeof,赋值将是


b + = 5 *((void *)(&(a [1 ])) - (void *)(&(a [0]));


看起来更复杂。


无论如何,任何填充都被纳入sizeof?
Is this valid?

int a[20];
void *b;

b = (void *)a; *// b points to a[0]

b += 5*sizeof(*a); // b points to a[5]

a[5] = 100;

printf( "%d\n" , *((int *)b) ); // prints 100

If so, if a had been a struct, would it still work?

Is there a possibility that the array could contain some padding, so
rather than sizeof, the assignment would be

b += 5*( (void *)(&(a[1])) - (void *)(&(a[0]));

which seems more more complex.

Would any padding be incorporated into sizeof anyway?



b + = 5 * sizeof(* a); // b指向a [5]

是*不正确*


当你这么做时

- char * c; c ++; == c增加1

--long int * x; x ++ == x增加4.

--void * p; p ++ //不正确==因为编译器不知道是什么

金额应该增加。


指针值递增或递减的数量是

,取决于对象指针的类型指向这个号码有一个

特定的名字,我不记得了,但是这个s的东西没有定义

for void *


b + = 5 *((void *)(&(a [1])) - (void *)(&(a [0]));

这里也是+ =由于指针

而且操作无效,而且数组之间没有填充。


-

vIpIn



b += 5*sizeof(*a); // b points to a[5]
is *incorrect*

when you do
--char *c; c++; ==c is incremented by 1
--long int *x; x++ ==x is incremented by 4.
--void *p; p++ //incorrect ==because compiler doesn''t know by what
amount should it increase.

the number by which a pointer value is incremented or decremented is
dependent on type of object pointer is pointing to. This number has a
specific name which i don''t remember, but this thing is not defined
for void *

b += 5*( (void *)(&(a[1])) - (void *)(&(a[0]));
here also "+=" operation will be invalid due to pointer
moreover there is no padding in between the arrays.

--
vIpIn



9月16日晚上7:13,sh.vi ... @ gmail.com写道:
On Sep 16, 7:13 pm, sh.vi...@gmail.com wrote:

当你这样做时

- char * c; C ++; == c增加1

--long int * x; x ++ == x增加4.
when you do
--char *c; c++; ==c is incremented by 1
--long int *x; x++ ==x is incremented by 4.



错误,假设x已初始化,则会增加1.


这里的证明:


long int i [1],* p = i,* q =& i [1];

printf ("%d \ n",(int)(q - p));


将始终打印1.

Wrong, assuming x was initialized, it would be incremented by 1.

Here''s proof:

long int i[1], *p = i, *q = &i[1];
printf("%d\n", (int)(q - p));

Will always print 1.


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

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