C编程void指针的概念 [英] Concept of void pointer in C programming

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

问题描述

是否有可能在C语言编程取消引用没有空指针的类型转换?

此外,有没有推广是可以接收的指针并将其存储在空指针的函数通过使用空指针,我们可以做一个广义的功能和什么办法?

对于例如。

 无效ABC(无效* A,INT B)
{
   如果(B == 1)
      的printf(%d个*(INT *)一); //如果收到整数指针
   否则如果(B == 2)
      的printf(%C,*(字符*)一); //如果字符指针接收
   否则,如果(二== 3)
      的printf(%F,*(*浮动)一); //如果浮动指针接收
}

我想使这个功能一般,不使用if-else语句,这可能吗?

此外,如果没有足以解释void指针的概念,一些互联网文章,那么这将是有益的,如果你能提供的网址。

此外,是指针算法可能出现的空指针?


解决方案

  

是否有可能取消引用没有void类型的指针铸造用C编程语言...


没有,无效表示没有类型的,这是不是你可以提领或指定。


  

是有推广可以接收指针,并将其存储在空隙指针的函数的任何方式和通过使用空指针我们可以作出广义功能..


您不能只是解引用它在一个可移植的方式,因为它可能无法正确​​对齐。它可以是在如ARM,其中指针的数据类型必须在数据类型的大小的边界对齐某些架构的问题(例如,指针32位整数必须在4字节边界对齐被解除引用)。

例如,读 uint16_t 无效*

  / *可能会收到错误的值,如果ptr不对齐的2字节* /
uint16_t值= *(* uint16_t)PTR;
/ *读取一个小尾数值可移植的方式* /
uint16_t值= *(* uint8_t有)PTR
                | ((*(uint8_t有*)(PTR +1))≤;&下; 8);


  

此外,是指针算法可能出现的空指针...


指针算法是不可能对无效的指针,由于缺乏指针下面具体数值,因此大小。

 无效* P = ...
无效* P2 = P + 1; / *究竟是无效的大小? * /

Is it possible to dereference the void pointer without type-casting in C programming language?

Also, is there is any way of generalizing a function which can receive a pointer and store it in void pointer and by using that void pointer we can make a generalized function?

for e.g.

void abc(void *a, int b)
{
   if(b==1)
      printf("%d",*(int*)a);     // If integer pointer is received
   else if(b==2)
      printf("%c",*(char*)a);     // If character pointer is received
   else if(b==3)
      printf("%f",*(float*)a);     // If float pointer is received
}

I want to make this function generic, without using if-else statements, is it possible?

Also if there are some internet articles which explain the concept of void pointer, then it would be beneficial if you could provide the URLs.

Also, is pointer arithmetic with void pointers possible?

解决方案

Is it possible to dereference the void pointer without type-casting in C programming language...

No, void indicates the absence of type, it is not something you can dereference or assign to.

is there is any way of generalizing a function which can receive pointer and store it in void pointer and by using that void pointer we can make a generalized function..

You cannot just dereference it in a portable way, as it may not be properly aligned. It may be an issue on some architectures like ARM, where pointer to a data type must be aligned at boundary of the size of data type (e.g. pointer to 32-bit integer must be aligned at 4-byte boundary to be dereferenced).

For example, reading uint16_t from void*:

/* may receive wrong value if ptr is not 2-byte aligned */
uint16_t value = *(uint16_t*)ptr;
/* portable way of reading a little-endian value */
uint16_t value = *(uint8_t*)ptr
                | ((*(uint8_t*)(ptr+1))<<8);

Also, is pointer arithmetic with void pointers possible...

Pointer arithmetic is not possible on pointers of void due to lack of concrete value underneath the pointer and hence the size.

void* p = ...
void *p2 = p + 1; /* what exactly is the size of void?? */

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

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