1

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:

Console print out

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.

6
  • string isn't C. Your array has no size, so writing to it corrupts the stack. Commented Aug 25, 2020 at 15:43
  • At first sight it looks like you increase the number of winners but not the array associated with it. Commented Aug 25, 2020 at 15:44
  • you are not allocating memory for your array of winners. . You could declare winner as string winners [100] or whatever the max number of winners are. Commented Aug 25, 2020 at 15:46
  • 2
    @MarkTolonen string in the context of CS50 is a typedef to char *. Yes, it's an utter abomination that screws up a lot of C students. Commented Aug 25, 2020 at 15:55
  • 2
    Please do not add pictures of text. A simple copy&paste is sufficient. Commented Aug 25, 2020 at 15:57

2 Answers 2

2

Your array winners has size 0 because you have initialized it like:

string winners [] = {};

Therefore there is no valid index i which would allow you to write winners[i]. Give a proper initial size to your array. Also, what exactly is 'string' in C?

Sign up to request clarification or add additional context in comments.

3 Comments

Because the number of winners is not predictable it should be done dynamically. My guess is that 'string' is just a typedef of something like char*.
candidate_count would always be big enough as initial size
Thanks for the answer! Makes sense, I'm coming from Javascript so used to being able to define an empty array and populate it as needed, which I appreciate is antithetical to the nature of C. As other comments have pointed out string is just a typedef that CS50 provide to make things easier at first.
1

C does not allow zero-length arrays, so you are relying on an extension provided by your compiler for this to be accepted at all:

        string winners [] = {};

But that having been accepted, it is of no use to you, because the dimensions of C arrays are not adjustable. Thus, any attempt to access an element of your array winners overruns its bounds, producing undefined behavior. Data corruption such as you observe is one of the common manifestations of such UB.

Resolve the issue by declaring the array large enough for the largest case you want to support (and by rejecting larger cases explicitly), or else by allocating memory for it dynamically, and increasing the allocation when you find you need to do.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.