Cprogramming 简明教程

void Pointer in C

void Pointer in C

C 中的 void pointer 是一种未与任何数据类型关联的指针类型。void 指针可以容纳任何类型的地址,并且可以转换为任何类型。它们也称为通用或泛型指针。

A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers.

在 C 编程中,函数 malloc()calloc() 返回“ void * ”或泛型指针。

In C programming, the function malloc() and calloc() return "void *" or generic pointers.

Declaring void Pointer

这是用于声明 void 指针的语法 −

This is the syntax you would use to declare a void pointer −

void *ptr;

Example of void Pointer

以下示例显示了如何在 C 程序中使用 void 指针 −

The following example shows how you can use a void pointer in a C program −

#include <stdio.h>

int main(){

   int a = 10;
   char b = 'x';

   // void pointer holds address of int a
   void *ptr = &a;
   printf("Address of 'a': %d", &a);
   printf("\nVoid pointer points to: %d", ptr);

   // it now points to char b
   ptr = &b;
   printf("\nAddress of 'b': %d", &b);
   printf("\nVoid pointer points to: %d", ptr);
}

Output

运行代码并检查其输出:

Run the code and check its output −

Address of 'a': 853377452
Void pointer points to: 853377452
Address of 'b': 853377451
Void pointer points to: 853377451

An Array of void Pointers

我们可以声明 void pointersarray 并存储指向不同 data types 的指针。

We can declare an array of void pointers and store pointers to different data types.

void 指针是指针,在 C 中可以容纳任何数据类型的内存地址。因此,void 指针数组是可以存储内存地址的数组,但这些地址可以指向不同数据类型的变量。

A void pointer is a pointer that can hold the memory address of any data type in C. Hence, an array of void pointers is an array that can store memory addresses, but these addresses can point to variables of different data types.

您还可以将 pointers 存储到任何用户定义的数据类型(例如数组和 structures )中。

You can also store pointers to any user-defined data types such as arrays and structures in a void pointer.

Example

在此示例程序中,我们声明了一个空指针数组并将其存储在每个下标的不同类型(int、float 和 char *)变量的指针。

In this example program, we have declared an array of void pointers and stored in it the pointers to variables of different types (int, float, and char *) in each of its subscripts.

#include <stdio.h>

int main(){

   void *arr[3];

   int a = 100;
   float b = 20.5;
   char *c = "Hello";

   arr[0] = &a;
   arr[1] = &b;
   arr[2] = &c;

   printf("Integer: %d\n", *((int *)arr[0]));
   printf("Float: %f\n", *((float *)arr[1]));
   printf("String: %s\n", *((char **)arr[2]));

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Integer: 100
Float: 20.500000
String: Hello

Application of void Pointers

下面列出了一些空指针的常见应用 −

Some of the common applications of void pointers are listed below −

  1. The malloc() function is available as a library function in the header file stdlib.h. It dynamically allocates a block of memory during the runtime of a program. Normal declaration of variables causes the memory to be allocated at the compile time.

void *malloc(size_t size);

  1. Void pointers are used to implement generic functions. The dynamic allocation functions malloc() and calloc() return "void *" type and this feature allows these functions to be used to allocate memory of any data type.

  2. The most common application of void pointers is in the implementation of data structures such as linked lists, trees, and queues, i.e., dynamic data structures.

Limitations of void Pointer

空指针具有以下限制:

The void pointer has the following limitations −

  1. Pointer arithmetic is not possible with void pointer due to its concrete size.

  2. It can’t be used as dereferenced.

  3. A void pointer cannot work with increment or decrement operators because it doesn’t have a specific type.