How can a function create a struct?

2019-09-07 13:46发布

问题:

One of my assignments for the C Programming course was define a function called create_card. This function receives a suit character and an a card value, and returns a card_t struct.

Question: How is a function supposed to create a struct? Can't it only create values for the struct? Did I misinterpret the meaning of the question or was the assignment written down wrong?

回答1:

This is an example of a function returning a struct.

struct test {
    int a;
};
struct test Fun(int k) {
    struct test d;
    d.a=k;
    return d;
}

Replace struct test with the name of your struct and the definition of struct test with your struct's definition.

How to use

int main() {
    struct test Test=Fun(6);
    printf("%d",Test.a); // prints '6'
    return 0;
}


回答2:

You can either return a struct from a function like in ForceBru's answer, or you can create a struct in C dynamic memory (a.k.a. the heap), using malloc, and return a pointer to it, e.g.

struct foo_st {
  int num;
  const char* str;
};

struct foo_st* 
/* the second argument s should practically be a literal string */
make_foo (int n, const char* s) {
   struct foo_st* p = malloc(sizeof(struct foo_st));
   if (!p) { perror("malloc foo"); exit(EXIT_FAILURE); };
   p->num = n;
   p->str = s;
   return p;
}

Your main (or some other function) might later do struct foo_st*fooptr = make_foo(32, "abc"); but someone should call free(fooptr) (or at least, free the address which has been inside fooptr).

Of course, you should never forget to free a malloc-ed pointer when it becomes useless. Be afraid of memory leaks, buffer overflow and undefined behavior. Read more about malloc(3) & free.

BTW, in practice you should decide of who is responsible for free-ing memory. In the above make_foo example, the second parameter to make_foo is supposed to be a literal string (if it is malloc-ed e.g. using strdup(3), you'll need to free it elsewhere, and that becomes very messy).

In practice you should document the conventions about who is responsible to free some dynamically previously mallocated memory. You might want to use valgrind (if your system has it), and, if using a recent GCC compiler, its -fsanitize=address option to hunt memory related bugs. Very often, you happen to code both making and destroying functions (like here or here).

You might want to read about garbage collection (at least, to understand the concepts, such as reference counting, and the terminology). Perhaps you'll later want to use Boehm's conservative garbage collector.



标签: c struct