#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 define a struct where the return type is mentioned. struct s is defined successfully.
Is this a valid usage? I never seen real scenario like this.
How to make this program to work??
I’m getting this compiler error:
asd.c: In function ‘main’:
asd.c:21:15: error: lvalue required as unary ‘&’ operand
Everything apart from your
memcpyline is correct (albeit a bit hard to read), and the compiler error tells you what’s wrong: You can’t take the address of a “temporary” (i.e. of the result of a function call expression).You could and should however just write the much more natural way: