pointer - segmentation fault

This is a discussion on pointer - segmentation fault within the C Programming forums, part of the General Programming Boards category; Hello This function lookAround searches for the nearest enemy. When it has found this enemy i want to "return" this ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    pointer - segmentation fault

    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;
    				}
    			}
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    In the function lookAround, closestEnemy is a local variable. If you set it to point to something, the change will only remain as long as you're still within the function. If you actually want to change what it points to, you need to pass it like this:
    Code:
    char **closestEnemy

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    thanx for your help
    I changed my code :
    Now i get an warning : assignment from incompatible pointer type
    But I dont understand wy
    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("%c\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;
    				}
    			}
    		}
    	}
    }

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Because closestEnemy is now a pointer to a pointer, when assigning a value to it in the lookAround function you need to dereference it first..(*clostestEnemy) = whateverValue..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Ok i changed my code but I still have a segmentation fault.
    Did I interprete your advise wrong? Because I can print the value of the double pointer in the function but when i try to print it in the main I get a segmentation fault

    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("%c\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;
    	char *pclosestEnemy;
    	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 )
    				{
    					pclosestEnemy = &array[i][j];
    					closestEnemy = &pclosestEnemy;
    					printf("%c\n", **closestEnemy);
    
    					isTrue = TRUE;
    				}
    			}
    		}
    	}
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    closestEnemy should not be ** in main(). Here's an example of how this works:

    Code:
    #include <stdio.h>
    
    void testfunc (int **ptp, int ray[3]) {
    	*ptp = &ray[1];
    }
    
    int main() {
    	int ray[3] = { 1, 2, 3 }, *ptr;
    
    	testfunc(&ptr, ray);
    
    	printf("%d\n",*ptr);
    
    	return 0;
    }
    You are submitting the address of the ptr (&), which is the same thing as a "pointer to a pointer". When that is dereferenced (*) in testfunc to yield a value, that value is the address pointed to by ptr -- which you can reassign that (eg, to the address of ray[1]).
    Last edited by MK27; 02-23-2010 at 04:17 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thanx it works

    that example helped me a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  2. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 08:26 AM
  3. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM
  4. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 06:53 AM
  5. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 11:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21