从struct创建一个函数

时间:2011-10-18 07:42:03

标签: c struct

是否可以从struct创建一个函数?

像这样:

#include <stdio.h>

struct dma
{
    int day, mes, year;
};

dma *x(int a, int b, int c)
{
}

int main(int argc, char *argv[])
{
}

并且,此函数返回一个struct ..

如果可能,我该如何使用它们?

1 个答案:

答案 0 :(得分:2)

不确定你想要完成什么,但也许这已足够接近了?

struct dma
{
    int day, mes, year;
};

struct dma *x(int a, int b, int c)
{
    struct dma *res = (struct dma *)malloc(sizeof(struct dma));
    res->day = a;
    res->mes = b;
    res->year = c;
    return res;
}

int main(int argc, char *argv[])
{
    struct dma *m = x(1, 2, 3);

    printf("Year: %d\n", m->year);

    free(m);
    return 0;
}