Using pointers to change a matrix

c program to copy one array to another array using pointers
changing array values using pointers
array of pointers
traversing an array using pointer in c
change value of array in c
size of array using pointer
2d array pointer c
write a c program to swap two arrays using pointers.

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.


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.

Using Pointers with Arrays, C takes care of the details of element size. You can copy the array a into b using pointers as well. The following code can replace (for i=0; i<MAX; a[i]=b[i], i++); :. Note: You can use any of the two notations int matrix[][COLS] or int (*matrix)[COLS], to access two dimensional array using pointers. The first int matrix[][COLS] is general array notation. Whereas int (*matrix)[COLS] is a pointer to array.


@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.

changing array values using pointers, changing array values using pointers. I am trying to traverse an array of chars with pointers. Every x characters, i want to make it a capitalized  Matrix Operations with Pointers is C program which will implement matrix operations such as addition, multiplication, subtraction etc. This matrix operations program works using console where user needs to provide matrix numeric values and later using the menu it can perform operations with results.


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

[PDF] Lecture 06 2D Arrays & pointer to a pointer(**) More about 2D arrays, Accessing a 2D array using pointers the name of the array A, is viewed as a pointer (const) to function foo and function can now change the value of the. How to multiply two matrix using pointers? In previous posts we learned to access a multi-dimensional array using pointer. Here in this post we will continue our learning further and learn to multiply two matrices using pointers. Logic of this program won't be any different from the program to multiply two matrix using array notation. In this


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

4. Pointers and Arrays, A two-dimensional array is not to be confused with an array of pointers. techniques using the realloc function were used to support arrays whose sizes change. 6 Answers. You don’t need pointers to create a matrix. Any kind of matrix can be represented as an 1d array with “numRows * numColumns” elements, where index of an individual element would be “columnIndex + rowIndex*numColumns. In fact, any N-dimensional array can represented as a 1d array,.


C Program to Access Array Elements Using Pointer, In this example, you will learn to access elements of an array using a pointer. Passing a Matrix to a Function Using a Pointer As we know, we need not specify the number of rows when a matrix is passed to a function. Thus, the mat_func function given below specifies that a matrix having unknown number of rows, each having four elements, will be passed as the actual argument. void mat func (int a [], int m, int n);


How to access two dimensional array using pointers in C, So I have decided to write an article on how to access a multidimensional array with a pointer (Access two-dimensional array using pointers in C). I am assuming​  The a variable is a permanent pointer and can not be changed, but p is not subject to such restrictions. C actually encourages you to move it around using pointer arithmetic . For example, if you say p++; , the compiler knows that p points to an integer, so this statement increments p the appropriate number of bytes to move it to the next element of the array.


Strings as arrays, as pointers, and string.h, A disadvantage of creating strings using the character array syntax is that you must say Below is how you might use a character pointer to keep track of a string. Furthermore, the only way to change the contents of an array in C is to make  To do matrix multiplication in C, we have two possible ways, using pointer and without pointers, it can sub-divided into using functions and without using functions. We will be creating two programs here, one will be without using functions/pointers and the other one passes matrices to functions and uses pointers.