I'm working through CS50 and am currently working on a small function to determine the winner(s) of an election.
void print_winner(void)
{
int max_votes = 0;
int num_of_winners = 0;
string winners [] = {};
for (int i = 0; i < candidate_count; i++) {
//Determine if the candidate has enough votes to be considered a winner
if (candidates[i].votes >= max_votes) {
//If so, increment the number of winners
num_of_winners ++;
//Assign the winning candidate to the winners array
winners[i] = candidates[i].name;
//And reset the max number of votes required to now be considered a winner
max_votes = candidates[i].votes;
}
printf("number of winners %i\n", num_of_winners);
}
for (int i = 0; i < num_of_winners; i++) {
printf("winner %s\n", winners[i]);
}
return;
}
Whenever I run the function the counter seems to increment properly but on the last printout the number is clearly wrong:
I'm still learning C and from what I've read this sounds like either a memory allocation issue or some kind of integer overflow issue however I'm unsure what the issue is and how to fix it.

stringisn't C. Your array has no size, so writing to it corrupts the stack.string winners [100]or whatever the max number of winners are.stringin the context of CS50 is atypedeftochar *. Yes, it's an utter abomination that screws up a lot of C students.