Hello
This function lookAround searches for the nearest enemy. When it has found this enemy i want to "return" this enemy with a pointer. When I want to write this enemy in the main I get a segmentation fault.
Does anyone has an idea?
Code:
#include <stdio.h>
#include <stdlib.h>
/*#include "Simulation.h"*/
#include "_fgets.h"
void lookAround(int yCoordinate, int xCoordinate, int height, int width, char array[6][6], int radius, char enemy, char *closestEnemy);
int main (void)
{
/* printf("You’re a desperate man, Sean. Desperate men don’t come to talk. They come to kill\n");*/
char array[6][6];
int i, j;
char *closestEnemy;
for (i = 0; i < 6; i++){
for (j = 0; j < 6; j++)
array[i][j] = 'b';
}
array[2][4] = 'a';
lookAround(2, 2, 6, 6, array, 2, 'a', closestEnemy);
printf("%i\n", *closestEnemy);
return 0;
}
/* This function looks in the world and returns the closest enemy */
void lookAround(int yCoordinate, int xCoordinate, int height, int width, char array[6][6], int radius, char enemy, char *closestEnemy)
{
int i, j, k, beginY, beginX, endY, endX;
boolean isTrue = FALSE;
for (k = 1; k <= radius; k++)
{
if ((yCoordinate - k) > 0)
beginY = yCoordinate - k;
else
beginY = 0;
if ((yCoordinate + k) < height)
endY = yCoordinate + k + 1;
else
endY = height;
if ((xCoordinate - k) > 0)
beginX = xCoordinate - k;
else
beginX = 0;
if ((xCoordinate + k) < width)
endX = xCoordinate + k + 1;
else
endX = width;
for (i = beginX; i < endX && isTrue == FALSE; i++)
{
for (j = beginY; j < endY && isTrue == FALSE; j++)
{
if (array[i][j] == enemy )
{
printf("%c\n", array[i][j]);
closestEnemy = &array[i][j];
isTrue = TRUE;
}
}
}
}
}