programing in C. I am having a hard time figuring out how to pass a 2D array, bo
ID: 3574784 • Letter: P
Question
programing in C.
I am having a hard time figuring out how to pass a 2D array, board and numbers from the main function to the get valid input to the col erase function. Can someone fix the pointer errors in the function definaions and calls?
void col_erase(int rowerase, char** board, int num_rows, int num_cols){
int row = 0;
free(board[rowerase]);
while(row<num_rows)
{
board[row] = board[row+1];
row++;
}
}
char get_valid_input(char* userInput, char** board, int num_rows, int num_cols)
{
if(userInput[0] == 'd'){
int number = atoi(&userInput[4]);
if(userInput[2] == 'c' ){
col_erase(number, char**board, int num_rows, int num_cols);
}
}
}
int main(int argc, char** argv)
{
int num_rows=0, num_cols=0;
char** board;
char* command;
read_args(argc, argv, &num_rows, &num_cols);
create_board(&board, num_rows, num_cols);
command = (char*)malloc(10*sizeof(char));
do
{
printf("Enter your command: ");
fgets(command, 100, stdin);
}while((get_valid_input(command,board, num_rows, num_cols) != 'q'));
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void col_erase(int rowerase, char** board, int num_rows, int num_cols){
int row = 0;
free(board[rowerase]);
while(row<num_rows)
{
board[row] = board[row+1];
row++;
}
}
char get_valid_input(char* userInput, char** board, int num_rows, int num_cols)
{
if(userInput[0] == 'd'){
int number = atoi(&userInput[4]);
if(userInput[2] == 'c' ){
// col_erase(number, char**board, int num_rows, int num_cols);
col_erase(number,board,num_rows,num_cols);
}
}
return userInput[0];
}
int main(int argc, char** argv)
{
int num_rows=0, num_cols=0;
char** board;
char* command;
read_args(argc, argv, &num_rows, &num_cols);
create_board(&board, num_rows, num_cols);
command = (char*)malloc(10*sizeof(char));
char c;
do
{
printf("Enter your command: ");
//fgets(command, 100, stdin);
scanf("%s",command);//modified
c=get_valid_input(command,board, num_rows, num_cols);
}
while( c!= 'q');
}
//errors are cleared/// but you have not sent complete code...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.