i currently have a array function that reads in values of user input and I want to print out the array using another function.
this is my code
Function to read in user input and store in 2d array
void readMatrix(){
int arr[3][4];
int *ptr;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("row %d, col %d: ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
ptr = &arr;
}
}
function to print array stored in readMatrix() function
void printMatrix(){
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("row %d, col %d = %d\n", i + 1, j + 1, arr[i][j]);
}
}
}
int main(){
//function to read matrix
readMatrix();
//function to print matrix
printMatrix();
return 0;
}
readMatrixcontains a local variable namedarr. This variable can't be used from any other function. I assume thatarris also a global variable? Then please remove it, and instead declare it locally inside themainfunction, and pass it as arguments to the functions that need it. I also recommend you refresh the sections about variable scope and lifetime in your text-books.readMatrix(); //function to read matrix. You already gave the function a self-documenting name.main()and pass a pointer to it in the function calls.