hi guys,
I'm trying to implement a basic program which deals with structures and pointers. I have to insert data for various citizens, which is structure that have other structures as fields.
in this case my main objective is to create 2 general functions that will get the information for each field of the structures. One for the int type and the other for strings. Both functions have 2 arguments, the 1st one is a string that will ask the user the information that we will have to store and the 2nd is the destination of that information.
But these 2 functions are to be used inside other functions that will deal with a specific structure from the "main" structure, in this case CITIZEN.
I noticed that this code work for a single citizen, but I'm not sure if it's totally correct. Also I should generalize this code, let's say to 10 citizens for example. In that case i think I have to create an array of 10 CITIZEN structures and it has to do with pointers and their increment, but i'm not "seeing" how to do it.
if anyone could give me a hand with this I would be very grateful.
Thanks
#include <stdio.h>
#include <string.h>
typedef struct personal_info {
char name[30];
int age;
int height;
}PERSONAL_INFO;
typedef struct info_number {
int driver_licence;
int social_security_number;
int passport;
}INFO_NUMBER;
typedef struct address {
char street[20];
int door_number;
int zip_code;
}ADDRESS;
typedef struct citizen {
struct personal_info prs_inf;
struct info_number infnum;
struct address add;
}CITIZEN;
/* I'm not sure if the 2nd argument should be like this */
void get_str_struct(char *s, char *ptr) {
printf("%s",s);
gets(ptr); /* I know I shouldn't use gets */
}
/* Once again I'm not sure if the 2nd argument should be like this */
void get_int_struct(char *s, int *ptr) {
printf("%s", s);
scanf("%d", ptr);
}
void insert_personal_info(CITIZEN *ptr) {
get_str_struct("Insert Name: ", ptr->prs_inf.name);
get_int_struct("Insert age: ", &ptr->prs_inf.age);
get_int_struct("Insert height: ", &ptr->prs_inf.height);
}
main() {
CITIZEN cit;
insert_personal_info(&cit);
printf("Name = %s", cit.prs_inf.name);
printf("Age = %d", cit.prs_inf.age);
printf("Height = %d", cit.prs_inf.height);
}