Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c - How to return different types from a single function

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
", *ptr_int);
    fprintf(stdout, "char value = %c
", *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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...