I'm doing this board game that lets the players expand the board's columns or rows:
The board is a 2d array dynamically allocated.
If the player wanted, the game would add a line (or row).
To add a row I need to allocate more memory to it right? I keep failing at it, idk what to try anymore..
Here is the code that starts the board in main:
char** tabuleiro = malloc(sizeof(char*) * linhas);
for(i = 0;i < linhas; i++)
tabuleiro[i] = malloc(sizeof(char*) * colunas);
Here is the code that starts the aux in the function:
char** insere_jogada(char** tab, jogador j, int* linhas, int*colunas){
// (...)
char** aux = tab;
Here is the code that would add a row:
*linhas = *linhas + 1;
aux = realloc(*tab, sizeof(char*)*(*linhas));
//also tried: tab = malloc(sizeof(char*)*(*linhas));
if(aux==NULL){
printf("Error.\n");
return tab;
}
Here is the code that would add columns:
*colunas = *colunas + 1;
for(i = 0;i < *colunas; i++){
aux[i] = realloc(*tab, sizeof(char*)*(*colunas));
Whole function:
char** insere_jogada(char** tab, jogador j, int* linhas, int*colunas) {
int l,c,i;
char resposta, escolha;
char** aux = tab;
pede_espaco(&l,&c); // espaco q o jogador quer jogar
resposta = check_jogada(tab[l][c], j); //jogada
if(resposta=='A'){ // se escolher adicionar lin/col
j.lincol--; //fica sem oportunidade de adicionar mais linhas/colunas
escolha = pede_lincol(); // escolher entre adicionar linha ou coluna
if(escolha == 'L'){
printf("\nLines before %d",*linhas);
*linhas = *linhas + 1;
printf("\nLines after %d",*linhas);
// ----
aux = realloc(*tab, sizeof(char*)*(*linhas));
//also tried: tab = malloc(sizeof(char*)*(*linhas));
if(aux==NULL){
printf("Error.\n");
return tab;
}
//
}
else{
*colunas = *colunas + 1;
for(i = 0;i < *colunas; i++){
aux[i] = realloc(*tab, sizeof(char*)*(*colunas));
}
}
}
else{
tab[l][c] = resposta;
}
return aux;
}
PS: Thank you in advance for your time and patience. This is a new topic for me so forgive me if I'm making an obvious/stupid mistake. :)