Defining structure in the inverse function type

#include <stdio.h> #include <string.h> struct s { int data; } fun() { static struct s ss; ss.data = 20; return ss; } int main() { struct s ss; memcpy(&ss, &(fun()), sizeof(struct s)); printf("\n Data: :%d", ss.data); return 0; } 

In the above program, Im trying to determine the structure in which the return type is specified. struct s is determined successfully.

Is this a legitimate use? I have never seen a real scenario.

How to make this program work?

I get this compiler error:

 asd.c: In function 'main': asd.c:21:15: error: lvalue required as unary '&' operand 
+4
source share
1 answer

Everything except your memcpy line is correct (albeit a little hard to read), and a compiler error tells you what is wrong: you cannot accept the address of a "temporary" (i.e., the result of calling the function ex & shy; pres & shy; sion )

You could and should, however, write a much more natural way:

 struct s ss = fun(); 
+7
source

Source: https://habr.com/ru/post/1432008/


All Articles