I have an array with values initialized to an integer value. When I try to change the values and print to file, the code compiles but returns a "segmentation fault" error upon execution. Any thoughts would be much appreciated!
int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
...//code setting int "p" to some number between -100 and 100
if (p < 25 || p > -25)
{
int temp = p + 25;
int currentVal = theArray[temp];
theArray[temp] = currentVal + 1;
}
}
When I take out the step changing the "currentVal" there is no segmentation fault. Thanks in advance!
p + 25must be in the range 0..49, sopneeds to be at least -25, and at most 24. So try,if (p < 25 && p >= -25)