Hi,
I am a bit stumped by a small issue and I am sure that it is something simple I am overlooking and could use some direction. Below I have put my code where I define an array of structures and a pointer to it. What I am doing is taking the input and assigning a name to each Citizen in newCits. However when I am testing the code and I print out the names, they all come out the same. Any help is much appreciated!
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *name; } Citizen; void mainMenu(Citizen *c); void uploadMenu(Citizen *c); void citizenUpload(Citizen *c); void printCitizens(Citizen *c); void inputFlush(); int main(){ Citizen newCits[5]; Citizen *p = newCits; mainMenu(p); return (0); } void mainMenu(Citizen *c){ int input; printf("Welcome to the HGSM Main Menu! What would you like to do?\n(1) Access Upload Menu\nSelection: "); scanf("%d", &input); inputFlush(); if (input == 1){ uploadMenu(c); } } void uploadMenu(Citizen *c){ int input; printf("Welcome to the HGSM Upload Menu. What would you like to do?\n(1) Upload Citizen Data\nSelection: "); scanf("%d", &input); inputFlush(); if (input == 1){ citizenUpload(c); } } void citizenUpload(Citizen *c){ int input, i; char citInput[20]; printf("Welcome to the Citizen Upload Menu. How many Citizens do you want to Upload?\nNumber of Citizens: "); scanf("%d", &input); inputFlush(); for (i = 0; i < input; i++){ printf("Enter Citizen Information in the format (Name, District #, Year of Birth, Sex):\nInput: "); scanf("%s", citInput); inputFlush(); (c+i) -> name = citInput; } printCitizens(c); } void printCitizens(Citizen *c){ int i; for (i = 0; i < 2; i++){ printf("%s\n", (c+i) -> name); } } void inputFlush(){ int ch; while ((ch = getchar()) != '\n' && ch != EOF); }



LinkBack URL
About LinkBacks


