fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6. int ID;
  7. char Name [20];
  8. } rec;
  9.  
  10. struct rec* addEntry(){
  11. rec *temp;
  12. temp = malloc(sizeof(rec));
  13. printf("Please give me an ID number\n");
  14. temp->ID = 42; // Fake
  15.  
  16. printf("Now give me a name, maximum size for this is 20 characters\n");
  17. strcpy(temp->Name, "Paul"); // Fake
  18. return temp;
  19. }
  20.  
  21. struct rec** returnPointerToPointerArray()
  22. {
  23. rec **pointerArray;
  24. pointerArray = malloc(sizeof(rec*)*2); // Allocates 2 pointers
  25. pointerArray[0] = addEntry();
  26. pointerArray[1] = addEntry();
  27. printf("ID in main : %d\n", pointerArray[0]->ID);
  28. printf("Name in main : %s\n", pointerArray[0]->Name);
  29. printf("ID in main : %d\n", pointerArray[1]->ID);
  30. printf("Name in main : %s\n", pointerArray[1]->Name);
  31.  
  32. return pointerArray;
  33. }
  34.  
  35. int main(void) {
  36. struct rec** ptr = returnPointerToPointerArray();
  37.  
  38. // Now free the memory :)
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Please give me an ID number
Now give me a name, maximum size for this is 20 characters
Please give me an ID number
Now give me a name, maximum size for this is 20 characters
ID in main : 42
Name in main : Paul
ID in main : 42
Name in main : Paul