0

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
    would help if you post code – Sarang Apr 22 '15 at 17:12
  • I don't have any code. It's an assignment. They give you parameters to follow and you have to design it yourself. – white lotus Apr 22 '15 at 17:13
  • Are you perhaps lost in linguistics? The function can not create a type (and hey, a struct is a type, so creating a struct means creating a type, right?) -- that's still something the programmer must do, in statically typed languages like C. It can create *objects* of certain types though, and that is probably the meaning of this in the pesky natural language we use. – Peter - Reinstate Monica Apr 22 '15 at 17:30
  • I wrote it verbatim as it was written on the assignment. I didn't know if the assignment had a typo or not. – white lotus Apr 22 '15 at 17:33

2 Answers2

4

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;
}
ForceBru
  • 43,482
  • 10
  • 63
  • 98
-1

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547