Avatar of David Sankovsky
David Sankovsky
Flag for Israel

asked on 

using pointers to pointers to write to a two dimensional array

Hi Fellas, I'm having some trouble with the following code. I'm trying to initiate a two dimensional array of chars with spaces using pointers and I keep getting an error, could you please help me figure out what I'm doing wrong?
#define _CRT_SECURE_NO_WARNINGS
/*-------------------------------------------------------------------------
  Include files:
--------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdbool.h>


/*=========================================================================
  Constants and definitions:
==========================================================================*/

/* put your #defines and typedefs here*/

#define ROWS 10
#define COLS 10
#define SUBS_NUM 4

void CreateBattlefieldwithpointers(char** Array) {
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			*(Array[i] + j) = ' ';
		}
	}
}

void PrintBattlefield (char battlefield[ROWS][COLS]) {
    for (int i=0, inum=ROWS-1 ; i<ROWS ; i++, inum--) {
        printf("%d | ",inum);
        for (int j=0, jnum=0 ; j<COLS ; j++, jnum++) {
            printf("%c ",battlefield[i][j]);
        }
        printf("\n");
    }
    printf("    - - - - - - - - - - \n    0 1 2 3 4 5 6 7 8 9 \n");
}

int main()
{
    char PlayerTable[ROWS][COLS], CompTable[ROWS][COLS];
    char** PT = &PlayerTable[0], CT = &CompTable[0];
    CreateBattlefieldwithpointers(&PT);
    PrintBattlefield(PlayerTable);
	 

    return 0;
}

Open in new window


I use the printing method with direct array pass intentionally for now, until I can get why I can't initialize the array the way I need.
CProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment
phoffric

8/22/2022 - Mon