I don't fully understand if it's normal to get struct element straight from function call which returns struct (or pointer to struct) like in the following lines of code:
function().num
ptr_function(&var)->num
Below is code example I wrote which compiles and runs without any issues on my system.
#include <string.h>
#include <stdio.h>
#define LEN 32
typedef struct
{
int num;
char str[LEN];
} myType;
myType function( void )
{
myType var;
var.num = 10;
strncpy( var.str, "Hello!", LEN );
return var;
}
myType* ptr_function( myType* var )
{
var->num = 20;
strncpy( var->str, "Hello ptr!", LEN );
return var;
}
int main()
{
/* 1st example */
printf( "%d %s\n", function().num, function().str );
/* 2nd example */
myType var;
printf( "%d %s\n", ptr_function(&var)->num, ptr_function(&var)->str );
return 0;
}
And it returns the following:
10 Hello!
20 Hello ptr!
So my question is: Are there any issues or undefined behaviour which will arise from doing the following in c99 or c11?
function().num
ptr_function(&var)->num