Access Violation

Hi,

I'm writing a tic-tac-toe game for a single player to play against a computer. When I try to compile, I get an error that says: "Unhandled exception at 0x01161473 in Work.exe: 0xC0000005: Access violation reading location 0xcccccc0a."

Could someone please help me figure out how to fix this?

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
void Player(char *playerOne,char *computerOne,char board[3][3]);
	{
        int randomnumber;                      
        int randvalue;  
        char dummy;
		char *playerOne;
		char *computerOne;
       
        int playerFirst;
   
        printf("\n Would you like to be X's or O's?\n");
        scanf("%c%c", &dummy, &playerOne);
                                                      //allow user to input value 'x' or 'o'....dummykey eats up ENTER key

        if (*playerOne == 'x')                                                                         //logical if to assign a computer 'x' or 'o'
            {                    
            *computerOne = 'o';                
            }
        else
            {
            *computerOne = 'x';
            }
Lines 7 and 8 hide the parameters passed to the function with invalid pointers of the same name.

I'm assuming you called the function with something like this:
1
2
3
4
5
char p,
    c,
    board[3][3];
//...
Player(&p,&c,board);
Yeah, that's essentially how I called it. I just included the function because of the character limit on posts.
This is a classic reason to avoid type unsafe functions like scanf.

scanf takes a pointer to whatever variable you want to fill.

assuming you have a character you want to fill, you'd do it like so:

1
2
char c;
scanf("%c",&c);


This fills c (a char) with the character.

What you're doing is a bit different:

1
2
char* ptr;
scanf("%c",&ptr);


Since you're giving a pointer to a pointer instead of a pointer to a char... this fills ptr (a pointer, not a char) with the character.

What you probably want to do is something like this:

 
scanf("%c",playerOne);  // notice, no &.  playerOne is already a pointer to the char 
Topic archived. No new replies allowed.