fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct A
  6. {
  7. char a, b, c, d, e, f ;
  8. };
  9.  
  10. #define DYNAMICALLY_SIZED 1
  11.  
  12. struct B
  13. {
  14. int sz ;
  15. char name[DYNAMICALLY_SIZED] ;
  16. };
  17.  
  18. struct B* make_b( const char* name )
  19. {
  20. struct B* pb = malloc( sizeof( struct B ) + strlen(name) ) ;
  21. if(pb) { pb->sz = strlen(name) ; strcpy( pb->name, name ) ; }
  22. return pb ;
  23. }
  24.  
  25. int main()
  26. {
  27. struct A a = { 'H', 'e', 'l', 'l', 'o', 0 } ;
  28. printf( "%s [%u]\n", &a, strlen(&a) ) ;
  29.  
  30. struct B* ptr = make_b( &a ) ;
  31. puts( ptr->name ) ;
  32. free(ptr) ;
  33.  
  34. return 0 ;
  35. }
  36.  
Success #stdin #stdout 0s 1920KB
stdin
Standard input is empty
stdout
Hello [5]
Hello