Advertisement
trishLEX

Untitled

Apr 30th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define MAX_SIZE 20
  7.  
  8. typedef struct User {
  9.     char *login;
  10.     char *password;
  11.     int id;
  12. } User;
  13.  
  14. void jsonUser(User *user) {
  15.     printf("{id: %d, login: \"%s\", password: \"%s\"}\n",
  16.             user->id, user->login, user->password);
  17. }
  18.  
  19. void freeUsersArray(User** users, unsigned size) {
  20.     unsigned i;
  21.     for (i = 0; i < size; i++) {
  22.         free((*users)[i].login);
  23.         free((*users)[i].password);
  24.     }
  25.     free(*users);
  26. }
  27.  
  28. void main() {
  29.     User *users = NULL;
  30.     unsigned size;
  31.     char buffer[128];
  32.     unsigned i;
  33.  
  34.     printf("Enter number of users: ");
  35.     scanf("%d", &size);
  36.  
  37.     size = size <= MAX_SIZE? size: MAX_SIZE;
  38.     users = (User*) malloc(size * sizeof(User));
  39.  
  40.     for (i = 0; i < size; i++) {
  41.         printf("user #%d\nname: ", i);
  42.         scanf("%127s", buffer);
  43.         users[i].id = i;
  44.         users[i].login = (char*) malloc(strlen(buffer) + 1);
  45.         strcpy(users[i].login, buffer);
  46.         printf("password: ");
  47.         scanf("%127s", buffer);
  48.         users[i].password = (char*) malloc(strlen(buffer) + 1);
  49.         strcpy(users[i].password, buffer);
  50.     }
  51.  
  52.     for (i = 0; i < size; i++) {
  53.         jsonUser(&users[i]);
  54.     }
  55.  
  56.     freeUsersArray(&users, size);
  57.     getch();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement