I declared this in the main:
char** board;
int N;
.
.
.
fill_Board(board, N);
then I scanf the number N and using malloc I did dynamic allocation for the char** board like that:
void creat_board(char*** board,int N)
{
int i=0;
(*board)=(char**)malloc(N*sizeof(char*));
if (board==NULL)
{
printf("malloc failed!\n");
exit(1);
}
for (i=0;i<N;i++)
{
(*board)[i]=(char*)malloc(N*sizeof(char)); /*add the
if((*board)[i]==NULL)
{
printf("malloc failed!\n");
exit(1);
}
}
}
I want now to fill the board and to print it but its not working..here is what I tried to fill:
Void fill_Board(char** board,int N)
{
int i=0,j=0;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
board[i][j]='S';
}
}
}
when I tried to print it the values in the array (board) didnt change at all.why it that happening??
use pointer to array:
void fill_Board(size_t N, char (*board)[N][N])
{
for(size_t i = 0; i < N; i++)
{
for(size_t j = 0; j < N; j++)
{
(*board)[i][j]='S';
}
}
}
or more simple:
void fill_Board(size_t N, char board[N][N])
{
for(size_t i = 0; i < N; i++)
{
for(size_t j = 0; j < N; j++)
{
board[i][j]='S';
}
}
}
to dynamically allocate memory:
void *create_Board(size_t N)
{
return calloc(1, sizeof(char[N][N]));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.