Timeline for Why do many functions that return structures in C, actually return pointers to structures?
Current License: CC BY-SA 3.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 19, 2017 at 19:29 | comment | added | simpleuser | @JenniferAnderson you declare function prototypes (functions without bodies) in header files and can then call those functions in other code, without knowing the body of the functions, because the compiler just needs to know how to arrange the arguments, and how to accept the return value. By the time you link the program, you actually have to know the function definition (i.e. with a body), but you only need to process that once. If you use a non-simple type, it also needs to know that type's structure, but pointers are often the same size and it doesn't matter for a prototype's use. | |
| S Oct 19, 2017 at 15:18 | history | suggested | Solomon Slow | CC BY-SA 3.0 |
Use block quoting for whole paragraphs.
|
| Oct 19, 2017 at 15:04 | review | Suggested edits | |||
| S Oct 19, 2017 at 15:18 | |||||
| Oct 19, 2017 at 14:35 | comment | added | yoyo_fun | @amon So this is how declaring function headers (prototypes/signatures) before declaring how they work is actually done in C ? And it is possible to do the same thing to the structures and unions in C | |
| Oct 19, 2017 at 14:18 | comment | added | amon |
@JenniferAnderson C has a concept of incomplete types: a type name can be declared but not yet defined, so it's size is unavailable. I cannot declare variables of that type, but can declare pointers to that type, e.g. struct incomplete* foo(void). That way I can declare functions in a header, but only define the structs within a C file, thus allowing for encapsulation.
|
|
| Oct 19, 2017 at 14:17 | history | edited | Ryan | CC BY-SA 3.0 |
added 254 characters in body
|
| Oct 19, 2017 at 14:09 | comment | added | yoyo_fun | How is it possible to not know how much memory a certain variable will need if you already have the struct type defined? | |
| Oct 19, 2017 at 14:04 | history | answered | Ryan | CC BY-SA 3.0 |