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 malloc
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.