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?
You can either return a
struct
from a function like in ForceBru's answer, or you can create astruct
in C dynamic memory (a.k.a. the heap), usingmalloc
, and return a pointer to it, e.g.Your
main
(or some other function) might later dostruct foo_st*fooptr = make_foo(32, "abc");
but someone should callfree(fooptr)
(or at least,free
the address which has been insidefooptr
).Of course, you should never forget to
free
amalloc
-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 abovemake_foo
example, the second parameter tomake_foo
is supposed to be a literal string (if it ismalloc
-ed e.g. using strdup(3), you'll need tofree
it elsewhere, and that becomes very messy).In practice you should document the conventions about who is responsible to
free
some dynamically previouslymalloc
ated 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.
This is an example of a function returning a struct.
Replace
struct test
with the name of your struct and the definition ofstruct test
with your struct's definition.How to use