【发布时间】:2014-03-28 17:14:42
【问题描述】:
我有一个想法,void 函数并不意味着“不返回任何东西”,而是返回具有未知类型的东西,比如void *,它是一个指针,您可以将它与任何类型的数据一起使用。所以我写了下面的代码来确定:
#include <stdlib.h>
#include<stdio.h>
void x_function(void *);
void x_function(void *d)
{
printf("the integer value of d is %d \n " , *(int *)d );
printf("the string value of d is %s \n " , (char *)d );
printf("the character value of d is %c \n " , *(char *)d );
printf("the double value of d is %lf \n " , *(double *)d );
return 10;
}
int main()
{
int x = (int)x_function(520);
printf("The Value is : %d" , x);
return 0;
}
但编译器报错:
error: invalid use of void expression
int x = (int)x_function(520);
那我的想法错了吗?
void 函数只是“不返回任何内容的函数”吗?
【问题讨论】:
-
是的,你错了,你现在发现了!
-
好的,但是知道这一点,虽然你不能返回一个值,你可以在一个返回类型为
void的函数中使用return;。可以用来提前突破功能,时不时会变得真正有用。