在C和C++中,’void*’有什么区别
在学习C和C++编程语言中void函数的区别因素之前,让我们来看一些示例,并深入理解void函数在何处使用,我们可以从中得出什么用例等。
void函数的名字如其所示,在编程中没有任何实体。当调用void函数时,它不会返回任何内容给主要位置;在允许的操作完成后,控制权跳转到调用该过程的地方,这可以是计算、递归或打印任何内容到显示屏幕,然后我们将研究指针的作用并且检查’void*’在C和C++中有什么区别。
C++ void函数-1
// C++ code to demonstrate void()
// returning void()
// here we are writing down the C++ programming language to demonstrate the
// concept of C++ code to demonstrate void() and returning the function
// void() function as well
#include
using namespace std;
// the below small code snippet demonstrates the void function. It's the syntax
void work()
{
cout << "As we are expecting it to perform the void function has returned the void to the main control";
}
// Driver void() returning void work()
// this below line (driver void code) returns the
//void function to the function call from where it has been called in the code
void test()
{
// this below line returns the void function to the function call
// from where it has been called
return work();
}
// the main driver code functionality starts from here
int main()
{
// this below line simple calls the void function, which we have
// created just above our int main() function
test();
return 0;
}
C++ void函数-2
// C++ code to demonstrate void()
// returning void()
// here we are writing down the C++ programming language to demonstrate the
// concept of C++ code to demonstrate void() and returning the function
// void() function as well
#include
using namespace std;
// the below small code snippet demonstrates the void function. It's the syntax
void test()
{
// we are writing the C code display text from here
cout << "As we are expecting it to perform the void function has returned the void to the main control";
// this below line returns the void function to the function call
// from where it has been called
return (void)"Nope, it doesn't print anything!";
}
// the main driver code functionality starts from here
int main()
{
// this below line simple calls the void function, which we have
// created just above our int main() function
test();
return 0;
}
输出:
//output_is_for_both_C++_codes_written_above
As we expect it to perform, the void function has returned the void to the central control.
C 空类型函数
// C code to demonstrate void()
// returning void()
// here we are writing down the C programming language to demonstrate the
// concept of C code to demonstrate void() and returning the function
// void() function as well
#include
void display_function(void);
// the main driver code functionality starts from here
void main() {
// this below line simple calls the void function, which we have
// created just above our int main() function
display_function();
} //end of the void main function
void display_function() { // Definition of Display
printf("C void function print statement 1");
printf("C void function print statement 1 perfection in programming in C.\"\n");
}
输出:
C void function print statement 1C void function print statement 1 perfection in programming in C."
‘void*’ 在C和C++中有什么不同
在C编程语言中,允许将void*
指针分配给任何指针类型,而不需要转换,但C++不允许。我们必须在C++中显式地对void*
指针进行类型转换。
让我们看一下下面的示例;以下在C中是有效的,但在C++中不是:
void* pointer;
int *i = pointer; // this is an example of the Implicit conversion from void* to int*
同样地,
int *j = malloc(sizeof(int) * 5); // this is an example of the Implicit conversion from void* to int*
要使上面的代码在C++中编译通过,我们必须使用显式转换,如下所示,
void* pointer;
int *i = (int *) pointer;
int *j = (int *) malloc(sizeof(int) * 5);