3 ms·
C can return a struct from a function. That's multiple return values.
by jzwinck 11y ago
C can return a struct from a function. That's multiple return values.
- scott_s 11y agoYeah, but it's cumbersome because you have to define that struct elsewhere, and C does not have elegant mechanisms for destructing in-place.
- ratboy666 11y agoHow about #include <stdio.h> typedef struct mystruct { int x; char *p; } mystruct; mystruct func(int n) { return (mystruct) { n, NULL }; } int main(void) { mystruct v = func(1); printf("%d\n", v.x); printf("%d\n", func(2).x); return 0; } Works fine with gcc 5.3.1. Multiple destructuring is (maybe) problematic, but if you really want this.
- scott_s 11y agoThat has both downsides I mentioned.