Rotem Heinig
Rotem Heinig

Reputation: 9

Using pointers to change a matrix

In our c course, the teacher has given us a mini project to build a "Reversi" game. I'm having trouble building the board.

#define Size 8
int main()
{
    char Board[Size][Size] = { {" "} };
    resetBoard(Board);
    printBoard(Board);
    printf("\n");
    getch();
}
void resetBoard(int** board)
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            *(board + (i * Size + j)) = 'x';
        }
    }
}
void printBoard(int board[Size][Size])
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            printf("%c", board[i][j]);
            printf(" ");
        }
        printf("\n");
    }
}

I've checked the program and the program gets:

Run-time check failure #2 - Stack around the variable 'Board' was corrupted

when it changes the first X on the third row. For example, if I run the program till the end of the 2d row (16), I wont get this error.

Upvotes: 0

Views: 611

Answers (4)

Anders Cedronius
Anders Cedronius

Reputation: 2076

You got a bunch of errors in your code. See inline comments.

    //Use all capitals for defines
#define BOARD_SIZE 8

//Just reset the whole array to spaces.. No need to traverse byte by byte.
void resetBoard(char* board) {
    //memset version
    //memset(board, ' ', (BOARD_SIZE*BOARD_SIZE)*sizeof(char));

    //non memset version
    for (int i=0; i<(BOARD_SIZE*BOARD_SIZE); i++) *board++='x';
}

void printBoard(char *board) {
    for (int i = 0; i < BOARD_SIZE; i++){
        for (int j = 0; j < BOARD_SIZE; j++){
            //Access the 2D array like this (y * width of array + x)
            printf("%c", board[i*BOARD_SIZE+j]);
            printf(" ");
        }
        printf("\n");
    }
}

//Don't start a name using capitals.. Later when you program c++ or similar you will understand :-)
int main()
{
    //This is a more dynamic memory model and is not allocated on the stack.. (free when done!!)
    char *board=(char*)malloc(BOARD_SIZE*BOARD_SIZE);
    //there are several ways of working with arrays.. No need to complicate stuff if not needed.
    //Just point out the first byte of the array.. (See the methods takes a char pointer and that is what's passed the methods)
    if (board) {
        resetBoard(board);
        //Test to see if it works
        board[1*BOARD_SIZE+2]='0';
        printBoard(board);
        free(board);
    } else {
        printf("Out of memory!\n");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Or bling it like it's 2020!

#define B 16 //Define the size of the 2d matrix
void printMatrix(char*b){for(int i=-1;i<B*B-1;(++i+1)%B?printf("%c ",b[i%B*B+i/B]):printf("%c\n",b[i%B*B+i/B])){}} //Print the 2d matrix
int main(){
    char b[B*B]={[0 ...B*B-1]='x'}; //Reserve the 2d matrix space and set all entries to 'x'
    printMatrix(&b[0]); //pass the pointer to the print 2d matrix method
    return 0; //Done!
}

Or 2021 ;-) (A 2d array)

#define B 32
void p(char b[B][B]){for(int i=-1;i<B*B-1;(++i+1)%B?printf("%c ",b[i%B][i/B]):printf("%c\n",b[i%B][i/B])){}}
int main(){
    char b[B][B]={[0 ...B-1][0 ...B-1]='x'};
    p(&b[0]);
}

Upvotes: 1

eapetcho
eapetcho

Reputation: 527

below is the pointer version of my previous suggestion:

#include<stdio.h>
#include<stdlib.h>

#define Size 8

void resetBoard(char *board, int size);
void printBoard(char *board, int size);

int main()
{
    char *Board = (char *)malloc(Size*Size*sizeof(char));
    resetBoard(Board, Size);
    printBoard(Board, Size);
    printf("\n");
   free(Board);

    return 0;
}

void resetBoard(char *board, int size)
{
    for (size_t i = 0; i < size; i++)
    {
        for (size_t j = 0; j < size; j++)
        {
            *(board +i*size + j) = 'x';
        }
    }
 }

 void printBoard(char *board, int size)
 {
      for (size_t i = 0; i < size; i++)
      {
           for (size_t j = 0; j < size; j++)
           {
                printf("%c ", *(board +i*size + j));
           }
           printf("\n");
      }
  }

The compile (on my machine) looks again, like:

gcc -std=c11 -Wall reversi.c -o a.out

and the execution gives:

./a.out
x x x x x x x x
x x x x x x x x 
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x

Upvotes: 1

eapetcho
eapetcho

Reputation: 527

@RotemHeinig. Your code has many deficiencies. Bellow is an example reformating your example. Maybe, it will give you an idea of how to improve to work:

#include<stdio.h>

#define Size 8

void resetBoard(char board[][Size]);
void printBoard(char board[][Size]);

int main()
{
    char Board[Size][Size];
    resetBoard(Board);
    printBoard(Board);
    printf("\n");

    return 0;
}

void resetBoard(char board[][Size])
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            board[i][j] = 'x';
        }
    }
 }

 void printBoard(char board[][Size])
 {
     for (size_t i = 0; i < Size; i++)
     {
         for (size_t j = 0; j < Size; j++)
         {
              printf("%c ", board[i][j]);
              printf(" ");
         }
         printf("\n");
     }
  }

Compiling this code using gcc on my machine looks like:

gcc -std=c11 -Wall reversi.c -o a.out

And the execution gives:

./a.out
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x

I hope you find inspiration from here on.

Upvotes: 1

Jakub Hubert
Jakub Hubert

Reputation: 336

I think there may be problem with initialization of your board as type of char and working with pointers and array of type int in your functions. Char has size of 1 byte and int has bigger size depending on platform (4 bytes most of the times). This will cause memory problems with manipulation and looping over your array.

In your case, it looks like you have looped over whole allocated memory after 2 rows because you used pointers of type int. Int is in your case probably 4 times bigger than char resulting in looping over whole of your data structure of type char 4 times quicker as you expected.

Upvotes: 4

Related Questions