fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct person {
  4. int age;
  5. float weight;
  6. char *name;
  7. };
  8.  
  9. int main()
  10. {
  11. struct person *ptr;
  12. int i, num;
  13.  
  14. printf("Enter number of persons: ");
  15. scanf("%d", &num);
  16.  
  17. ptr = (struct person*) malloc(num * sizeof(struct person));
  18. // Above statement allocates the memory for n structures with pointer personPtr pointing to base address */
  19.  
  20. ptr->name=(char*)malloc(num*sizeof(char));
  21.  
  22. for(i = 0; i < num; ++i)
  23. {
  24. printf("Enter name, age and weight of the person respectively:\n");
  25. scanf("%s%d%f",&ptr->name[i], &(ptr+i)->age, &(ptr+i)->weight);
  26. }
  27.  
  28. printf("Displaying Infromation:\n");
  29. for(i = 0; i < num; ++i)
  30. printf("%s\t%d\t%.2f\n", ptr->name[i], (ptr+i)->age, (ptr+i)->weight);
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Enter number of persons: Displaying Infromation: