如何从一个函数返回不同的类型 [英] How to return different types from a single function
问题描述
我有以下c代码:
#include
#include< stdlib.h>
void * func(int a){
if(a == 3){
int a_int = 5;
int * ptr_int =& a_int;
return(void *)ptr_int;
}
else if(a == 4){
char a_char ='b';
char * ptr_char =& a_char;
return(void *)ptr_char;
}
else {
fprintf(stderr,返回值为NULL);
返回NULL;
$ b int main(int argc,char * argv []){
int * ptr_int =(int *)func(3);
char * ptr_char =(char *)func(4);
fprintf(stdout,int value =%d\\\
,* ptr_int);
fprintf(stdout,char value =%c\\\
,* ptr_char);
返回0;
}
但是当我使用gcc来测试这段代码,我有以下结果:
int value = 98
char值=
root @ coupure:/ home / bohao / Desktop / test1375#gcc test.c -o test $ b $ root root @ coupure:/ home / bohao / Desktop / test1375#./测试
int值= 98
char值=
root @ coupure:/ home / bohao / Desktop / test1375#gcc test.c -o test $ b $ root @ coupure:/ home / bohao / Desktop / test1375#./test
int value = 98
char value =
root @ coupure:/ home / bohao / Desktop / test1375#gcc test.c -o test
root @ coupure:/ home / bohao / Desktop / test1375#./test
int value = 98
char value =
root @ coupure:/ home / bohao / Desktop / test1375 #gcc test.c -o test $ b $ root root @ coupure:/ home / bohao / Desktop / test1375#./test
int value = 98
char value =
root @ coupure :/ home / bohao / Desktop / test1375#./test
int value = / home / bohao / Desktop / test1375#gcc test.c -o test $ b $ root root @ coupure: 98
char值= g
root @ coupure:/ home / bohao / Desktop / test1375#gcc test.c -o test $ b $ root root @ coupure:/ home / bohao / Desktop / test1375# ./test
int value = 98
char value =
root @ coupure:/ home / bohao / Desktop / test1375#gcc test.c -o test
root @ coupure: / home / bohao / Desktop / test1375#./test
int value = 98
char value =!
为什么我要 98 对于ptr_int和ptr_char的随机值?
我想要的是通用函数,它们可以返回不同类型的值而不是使用两个函数。那可能吗 ?如果是这样,该怎么办?
问题就在我看到的时候,您正试图 return
函数(范围)中的局部变量的地址,并试图从调用者访问返回的内存。在调用者中,内存无效,任何用法都会导致未定义的行为。
$ b
解决方案:您需要为指针( malloc()
/ calloc()
),您希望返回
来自函数。这将解决这个问题,因为动态分配的内存的生命周期是手动或直到程序终止,直到更早的时候,直到 free() -d。
话虽如此,这种方法并不好。如果你想返回多种类型中的一种,请为包含所有类型成员的 struct
>使用标志来标记类型。填充相应的成员变量,设置标志和 return
结构变量。为了更好,您可以实际使用 union
作为结构的成员,因为您一次只需要一种类型。有关工作代码,请参阅 @pmg 的其他答案。
I have the following c code :
#include <stdio.h>
#include <stdlib.h>
void *func(int a) {
if (a==3) {
int a_int = 5;
int *ptr_int = &a_int;
return (void *)ptr_int;
}
else if (a==4) {
char a_char = 'b';
char *ptr_char = &a_char;
return (void *)ptr_char;
}
else {
fprintf(stderr, "return value is NULL");
return NULL;
}
}
int main (int argc, char *argv[]) {
int *ptr_int = (int *)func(3);
char *ptr_char = (char *)func(4);
fprintf(stdout, "int value = %d\n", *ptr_int);
fprintf(stdout, "char value = %c\n", *ptr_char);
return 0;
}
But when I use gcc to test this code, I have the following results :
int value = 98 char value = � root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = g root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test root@coupure:/home/bohao/Desktop/test1375# ./test int value = 98 char value = !
Why do I have the 98 for ptr_int and the random value of ptr_char ?
What I want is to have a general function who can return different types of values instead of using two functions. Is that possible ? If so, how to do it ?
The problem as I see it, you're trying to return
the address of a local variable from a function (scope) and trying to access the returned memory from the caller. In the caller, the memory is invalid and any usage will lead to undefined behavior.
Solution: You need to use dynamic memory allocation for the pointer (malloc()
/ calloc()
) which you want to return
from the function. This will overcome the issue here, as the lifetime of the dynamically allocated memory is untill free()
-d manually or till program termination, whichever is earlier.
Having said that, this approach is not a good one. If all you want to return one of multiple types, go for a struct
containing members for all types and use a flag to mark the type. Fill the corresponding member variable, set the flag and return
the structure variable.
For better, you can actually use an union
as a member of the structure, as you only need one type at a time. For a working code, please refer to the other answer by @pmg.
这篇关于如何从一个函数返回不同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!