首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从单个函数返回不同类型

如何从单个函数返回不同类型
EN

Stack Overflow用户
提问于 2016-05-25 09:50:53
回答 5查看 7.8K关注 0票数 1

我有以下c代码:

代码语言:javascript
复制
#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; 
}

但是,当我使用gcc测试这段代码时,我得到了以下结果:

int值= 98 char值=�root@Cou纯净:/home/bohao/Desktop/test1375# gcc test.c -o测试根@Cou纯净:/home/bohao/Desktop/test1375#/test1375#/ test int值= 98 char值= 98 char值=root@Cou纯净:/home/bohao/Desktop/test1375#.c -o测试根@ -o :/home/bohao/Desktop/Desktop/test1375# ./test int值=98 char值=root@Cou纯净:/home/Desktop/ test 1375#-o测试根@-o:/home/bohao/Desktop/test1375# ./test int值= 98 char值=root@Cou纯净:/home/bohao/Desktop/test1375# gcc test.c -o test root@Cou纯净:/home/bohao/Desktop/test1375#test1375#//测试int值= 98 char值=root@Cou纯净:/home/bohao/Desktop/ test 1375# test.c -o测试根@纯:/home/bohao/Desktop/test1375# ./test int值= 98 char值=g root@Cou纯净:/home/bohao/Desktop/test1375#test.c-o测试根@-o:/home/bohao/Desktop/test1375#test1375# gcc test.c -o -o测试根@Cou纯净:/home/bohao/Desktop/test1375# ./test int值= 98 char value =root@Cou纯净:/home/bohao/Desktop/test1375#-o test.c -o测试根@Cou纯净:/home/bohao/Desktop/test1375# ./test 1375值= 98 char值!

为什么我有98表示ptr_int和随机值 of ptr_char?

我想要的是,一个通用函数,它可以返回不同类型的值,而不是使用两个函数。这有可能吗?如果是的话,怎么做?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-05-25 09:53:35

在我看来,问题在于,您试图从函数(作用域)中return局部变量的地址,并试图从调用方访问返回的内存。在调用者中,内存无效,任何使用都会导致未定义行为

解决方案:您需要为指针(malloc()/ calloc())使用动态内存分配,您希望从函数中对指针进行return。这将克服这里的问题,因为动态分配的内存的生存期是手动的直到free()-d,或者直到程序终止为止,无论哪个是更早的。

话虽如此,这种做法并不是一个好办法。如果您想返回多个类型中的一个,请选择包含所有类型的成员的struct,并使用一个标志标记该类型。填充相应的成员变量,设置标志和return结构变量。

更好的是,实际上可以使用union作为结构的成员,因为一次只需要一种类型。有关工作代码,请参阅另一个答案 by @pmg

票数 9
EN

Stack Overflow用户

发布于 2016-05-25 10:12:52

您可以返回(包含)联合的结构。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

union members {
    int *intp;
    char *charp;
    double doublev;
};
struct group {
    int lastunionmember;
    union members x;
};

struct group f1(void) {
    struct group r = {0};
    int choice = rand() % 3;

    if (choice == 0) {
        r.x.intp = malloc(sizeof (int)); // remember to free(intp) at some time
        *r.x.intp = 42;
        r.lastunionmember = 1;
    }
    if (choice == 1) {
        r.x.charp = malloc(42); // remember to free(charp) at some time
        strcpy(r.x.charp, "forty two");
        r.lastunionmember = 2;
    }
    if (choice == 2) {
        r.x.doublev = 3.14159;
        r.lastunionmember = 3;
    }
    return r;
}

int main(void) {
    struct group x;
    srand(time(0));
    for (int k = 0; k < 20; k++) {
        x = f1();
        switch (x.lastunionmember) {
            default: printf("invalid value\n"); break;
            case 1: printf("int value is %d\n", *x.x.intp); break;
            case 2: printf("string is \"%s\"\n", x.x.charp); break;
            case 3: printf("double value is %f\n", x.x.doublev); break;
        }
    }
}

在ideone运行的代码

票数 5
EN

Stack Overflow用户

发布于 2016-05-25 09:54:26

您的问题是要返回本地作用域变量的地址。您必须使用动态分配来执行您想要做的事情。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>

void *func(int a)
{
    void *retVal = NULL;

    if (a==3)
    {
        retVal = malloc(sizeof(int));
        if (retVal != NULL)
        {
            *((int *)(retVal)) = 5;
        }
    }
    else if (a==4)
    {
        retVal = malloc(sizeof(int));
        if (retVal != NULL)
        {
            *((char *)(retVal)) = 'b';
        }
    }
    else
    {
        fprintf(stderr, "return value is NULL");
    }

    return retVal;
}

int main ()
{
    int *ptr_int = func(3);
    char *ptr_char = func(4);

    fprintf(stdout, "int value = %d\n", *ptr_int);
    fprintf(stdout, "char value = %c\n", *ptr_char);

    free(ptr_int);
    free(ptr_char);

    return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37433525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档