Charles Bergeron
Charles Bergeron

Reputation: 87

How to print a 2d array with a function in C?

I'm trying to print a 2d array with a function, but I keep getting the error "pointer expected"

I'm trying to make a battleship-type grid. I'm fine with printing out the co-ordinate row and column, but I can't actually get the 2d array (which contains "." in every element) to print at all.

Any help would be appreciated, I'm very new to this. Thanks! :)

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

int length;
int width;
int i;
int j;
char invisible_board;


void board_setup(int *rows, int *columns){
    char *invisible_board[*rows][*columns];
char *player_board[*rows][*columns];

for (i = 0; i < *rows; i++){
    for (j = 0; j < *columns; j++){
        invisible_board[i][j] = ".";    //Sets all elements in hidden board to water
    }       
}

for (i = 0; i < *rows; i++){
    for (j = 0; j < *columns; j++){
        player_board[i][j] = ".";
    }
}
}

void display(int *rows, int *columns, char *invisible_board){

printf("   ");
for (i=1; i < *rows +1;i++){
    printf("%d  ",i);
}
printf("\n");                       //Prints top row of co-ordinates

for (i=1; i < *columns+1;i++){
    printf("%d ",i);
    for (j=0;j < *columns;j++){         //Prints left column of co-    ordinates and rows of game board
            printf(" %c ",invisible_board[i-1][j]);
        }
        printf("\n");
        }

}

int main(void){

    printf("Please enter the amount of rows in your board\n");
    scanf("%d",&length);
    printf("Please enter the amount of columns in your board\n");
    scanf("%d",&width);

    board_setup(&length,&width);
    display(&length,&width,&invisible_board);

    return (0);
}

Upvotes: 0

Views: 4921

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44298

this is the simplest changes I could make to your code to get you to working code.... now.... this isn't good code yet. But gets you started.

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

int length;
int width;
int i;
int j;
char invisible_board[100][100];   // dynamically allocate....
char player_board[100][100];   // dynamically allocate....


void board_setup(int *rows, int *columns){  
    for (i = 0; i < *rows; i++){
        for (j = 0; j < *columns; j++){
            invisible_board[i][j] = '.';    //Sets all elements in hidden board to water
        }       
    }

    for (i = 0; i < *rows; i++){
        for (j = 0; j < *columns; j++){
            player_board[i][j] = '.';
        }
    }
}

void display(int *rows, int *columns){

    printf("   ");
    for (i=1; i < *rows +1;i++){
        printf("%d  ",i);
    }
    printf("\n");                       //Prints top row of co-ordinates

    for (i=1; i < *columns+1;i++){
        printf("%d ",i);
        for (j=0;j < *columns;j++){         //Prints left column of co-    ordinates and rows of game board
            printf(" %c ",invisible_board[i-1][j]);
        }
        printf("\n");
    }

}

int main(void){

    printf("Please enter the amount of rows in your board\n");
    scanf("%d",&length);
    printf("Please enter the amount of columns in your board\n");
    scanf("%d",&width);

    board_setup(&length,&width);
    display(&length,&width);

    return (0);
}

Upvotes: 1

Related Questions