use malloc for struct

 #include <stdio.h>  
 #include <stdlib.h>  
   
 struct Healdirect  
 {  
    double m_tors;  
    double curvedir[3];  
 };  
   
 void showme(struct Healdirect *healdeci)  
 {     
    struct Healdirect test;  
   
    test.m_tors = healdeci->m_tors;  
    test.curvedir[0] = healdeci->curvedir[0];  
    test.curvedir[1] = healdeci->curvedir[1];  
    test.curvedir[2] = healdeci->curvedir[2];  
   
    printf("torsion: %.2f \n", test.m_tors);  
    printf("curvature direction: ");  
    for(int i=0; i<3; i++)  
       printf("%.2f ", test.curvedir[i]);  
    printf("\n");  
 }  
   
 void main()  
 {  
    //example of struct using pointer and malloc  
    struct Healdirect *hd = malloc(sizeof(hd));  
      
    //sample data   
    hd->m_tors = 0.24;  
   hd->curvedir[0] = 0.99;  
   hd->curvedir[1] = 0.88;  
   hd->curvedir[2] = 0.77;  
   
   //show sample data after the memory is filled  
   showme(hd);  
   
   //free the memory  
   if(hd) free(hd);  
 }  
   

No comments