I just wrote a small program to play with structures.This program works fine but i just have small doubt in one statement.Can anyone clarify me please?
#include<stdio.h> #include<string.h> #include<stdlib.h> struct mystr { int a; float b; char a1[10]; }; void fun(struct mystr *ptr1) { struct mystr *ptr; ptr=malloc(sizeof(struct mystr)); ptr->a=10; ptr->b=1662.3456; strcpy(ptr->a1,"xxxxxx"); *ptr1=*ptr; /* <<<<<<<<<<<- This assignment is fine? */ free(ptr); } void main() { struct mystr var1; memset(&var1,0,sizeof(struct mystr)); fun(&var1); printf("my data is %4d,%10.3f,%5s\n",var1.a,var1.b,var1.a1); } I know that i can just pass a pointer to the fun and print it free it. But i just wanted this program to be this way(passing structure variable address and filling it). Thanks in advance.