I am a beginner to C and am trying to get more familiar with arrays and the concept of manual memory allocation by doing simple exercises. I have been reading all the (many) questions on SO regarding the "Abort trap: 6" error, and though I've learned a lot, they haven't solved my issue.
Similar threads I checked out include:
"Abort trap: 6" running C program on a Mac
...and more, all slightly different than what I'm dealing with.
The problem seems to be that I'm writing to memory I don't have access to, but I thought that by making the array big enough when I declare it, I would avoid this issue. Evidently I was wrong!
The code is supposed to simply create an array that holds 100 ints (in positions 0 to 99), and assign each one the value of its position (i.e. the first item in the array should be the int 0, and the last should be the int 99). When I run this code, I get all the example printf statements as expected – with the correct values in them – but it's followed by a line saying "Abort trap: 6".
Could someone take a look at my code and tell me what I'm doing wrong to cause this error?
#include <stdio.h>
int main(void)
{
int obvs[101];
for (int i = 0; i < sizeof(obvs); i++)
{
obvs[i] = i;
}
printf("obvs[9] = %i\n", obvs[9]);
printf("obvs[13] = %i\n", obvs[13]);
printf("obvs[37] = %i\n", obvs[37]);
printf("obvs[74] = %i\n", obvs[74]);
printf("obvs[99] = %i\n", obvs[99]);
return 0;
}