franka
franka

Reputation: 1927

What part of this causes a floating point exception?

I would be most grateful if people could have a look over this snippet of code and let me know what could a possible cause for the floating point exception.

Info:

I am confused because there is no division, float or integer, that could cause this.

    for (count = 0; count < sizeof(branches); count++){

    if (fgets(line,sizeof(line),fp)==NULL)
     break;
    else {

    int branch_taken = line[16] - 48; 

    branches[count] = branch_taken;
     }   
    }

Upvotes: 0

Views: 766

Answers (1)

Paul R
Paul R

Reputation: 213180

sizeof(branches) is a size in bytes - you need to use a constant which represents the number of elements i.e. 200, otherwise you will be exceeding the bounds of your branches array, which will result in Undefined Behaviour.

Your code should probably look something like this:

#define NUM_BRANCHES 200

int branches[NUM_BRANCHES];

for (count = 0; count < NUM_BRANCHES; count++)
{
    ...

Upvotes: 7

Related Questions