For some reason I can't understand, when I am initializing the aim variable within the main function, I get a error bus error ./a.out
. However, if I initialize the aim variable outside the main function, it compiles with no error. I was able to complete the challenge but I am just curious why this is. I would greatly appreciate an explanation.
Here is my working code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int aim = 0;
int main(void)
{
int horizontal = 0;
int depth = 0;
int result = 0;
FILE *data;
data = fopen("data.txt", "r");
if (data == NULL)
{
printf("This file doesn't exist.\n");
return 1;
}
char* direction;
int num;
while(!feof(data))
{
fscanf(data, "%s %d", direction, &num);
// printf("%s ", direction);
// printf("%d\n", num);
// Take variables and do arithmetic
if ((strcmp(direction, "forward")) == 0)
{
horizontal += num;
depth += (aim * num);
}
else if ((strcmp(direction, "up")) == 0)
{
// depth -= num; // PART 1
aim -= num;
}
else if ((strcmp(direction, "down")) == 0)
{
// depth += num; // PART 1
aim += num;
}
}
result = horizontal * depth;
printf("Answer: %i\n", result);
fclose(data);
return 0;
}
(If you move the aim declaration to inside the main function, you should get the error I was receiving).