So basically here I am just seeing whether my previously stored input, that is temp->voterDetails[0].name
, has its value restored after another input.
Unfortunately it does not seem to store its value. Could you help me understanding why?
#include <stdio.h>
#include <stdlib.h>
struct Voter {
char *name;
};
struct Block{
// Creating a block containing 5 voter details
struct Voter voterDetails[5];
};
int main() {
int choice;
struct Block *Genesis = (struct Block *)malloc(sizeof(struct Block));
struct Block *temp = Genesis;
struct Block *newnode = (struct Block *)malloc(sizeof(struct Block));
char *nm;
int voteNum = 0;
int blockNumber = 1;
do{
printf("What do you want to do\n1. Cast a vote\n2. Check your vote\n3. Exit\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter name");
fgets(nm, 30, stdin);
fgets(nm, 30, stdin);
voteNum += 1;
temp->voterDetails[voteNum-1].name = nm;
puts(Genesis->voterDetails[0].name);
break;
}
} while (choice != 3);
return 0;
}