Advertisement
banovski

Structs, pointers, typedef

Apr 25th, 2022 (edited)
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.      typedef struct employee
  8.      {
  9.           char first_name[16];
  10.           char last_name[16];
  11.           float salary;
  12.           struct employee *next_record;
  13.      } employee;
  14.  
  15.      employee *jane = malloc(sizeof(employee));
  16.      employee *john = malloc(sizeof(employee));
  17.  
  18.      strcpy(jane->first_name, "Jane");
  19.      strcpy(jane->last_name, "Doe");
  20.      jane->salary = 1234;
  21.      jane->next_record = john;
  22.  
  23.      strcpy(john->first_name, "John");
  24.      strcpy(john->last_name, "Doe");
  25.      john->salary = 2345;
  26.      john->next_record = NULL;
  27.  
  28.      employee *current = jane;
  29.      do
  30.      {
  31.           printf("First name: %s\n", current->first_name);
  32.           printf("Last name: %s\n", current->last_name);
  33.           printf("Salary: %.4f\n", current->salary);
  34.           putchar('\n');
  35.           current = current->next_record;
  36.      }
  37.      while(current != NULL);
  38. }
  39.  
  40. /* First name: Jane
  41.  * Last name: Doe
  42.  * Salary: 1234.0000 */
  43.  
  44. /* First name: John
  45.  * Last name: Doe
  46.  * Salary: 2345.0000 */
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement