0

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;
}
5
  • 3
    Your readMatrix contains a local variable named arr. This variable can't be used from any other function. I assume that arr is also a global variable? Then please remove it, and instead declare it locally inside the main function, 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. Commented Nov 4, 2022 at 9:58
  • 2
    And please try to create a proper minimal reproducible example, and edit your question to show it to us. Commented Nov 4, 2022 at 9:59
  • 1
    You should remove useless comments like readMatrix(); //function to read matrix. You already gave the function a self-documenting name. Commented Nov 4, 2022 at 10:01
  • You can create the array in main() and pass a pointer to it in the function calls. Commented Nov 4, 2022 at 10:03
  • @IanAbbott yes this is what i want to do. May I know how do i do that? Is there any resources/examples? Commented Nov 4, 2022 at 10:06

2 Answers 2

2

int arr[3][4]; is a local variable only valid inside that function, so you can't pass it to anyone else. The easiest and best solution is to use caller allocation instead. You can also rewrite the functions to work with variable amounts of rows/cols.

With just a few tweaks to your code:

#include <stdio.h>

void readMatrix (int rows, int cols, int arr[rows][cols]){
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("row %d, col %d: ", i + 1, j + 1);
            scanf("%d", &arr[i][j]);
        }
    }
}

void printMatrix (int rows, int cols, int arr[rows][cols]){
    for (int i = 0; i < rows; ++i)
    {
        printf("{ ");
        for (int j = 0; j < cols; ++j)
        {
            printf("%2.d ", arr[i][j]);
        }
        printf("}\n");
    }
}

int main (void)
{
    int arr[3][4];
    readMatrix(3,4,arr);
    printMatrix(3,4,arr);
    return 0;
}

Input:

1 2 3 4 5 6 7 8 9 10 11 12

Output:

{  1  2  3  4 }
{  5  6  7  8 }
{  9 10 11 12 }

If you don't understand why this works, then "array decay" is the next best thing to study. What is array to pointer decay?

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help! I am actually trying to use pointers with arrays to get in input and output but I am unsure on how. Are there any resources/videos I can learn from?
@anono This is using pointers, behind the lines, because of the mentioned array decay. Each parameter like int arr[rows][cols] is actually silently replaced by the compiler with int (*arr)[cols], which is a pointer to the first array (first row). So the data in the caller gets changed. This should be explained by any decent C book, like Gustedt - Modern C - perhaps not the most beginner-friendly C book, but likely the most technically accurate and up to date one. And available for free as pdf.
@anono Although "array decay" is a common term you will read, it's worth mentioning that it is not a term that you will find in any C standard document. I think "array decay" refers to arrays in most expressions being converted to a pointer to the first element of the array. That is not what is happening to the array declared in the parameter of a function. The parameter declaration is silently replaced (or in C standard terminology it is "adjusted") to use a pointer type.
@IanAbbott Strictly speaking the C standard separates function declaration "adjustment" (6.7.6.3/7) from array "pointer conversion" (6.3.2.1/3). The rules are the same in either case, so it is not a useful distinction, particularly not for beginners. The informal term "decay" is therefore more suitable to cover both of these formal C rules at once.
@Lundin I don't like using the informal term "array decay" because it's never been entirely clear to me whether it can be used to describe both the array parameter type adjustment and the expression conversion, which are very different but complementary rules. (If I do use it, I tend to restrict it to describing the expression conversion since I want two different terms to describe the two different rules, but perhaps that is just a personal idiosyncrasy of mine.)
0

The problem is that you are trying to access the array from outside the scope of the function that created it. You need to pass it as a parameter to your print function. You can also return a pointer to the array from your read function and then pass the pointer to your print function. Here is an example of the second option:

#include <stdio.h>

int *readMatrix(int *arr);

void printMatrix(int *arr);

int main()
{
    int arr[3][4];

    readMatrix(&arr[0][0]);
    printMatrix(&arr[0][0]);

    return 0;
}

int *readMatrix(int *arr)
{
    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 * 4 + j]);
        }
    }

    return arr;
}

void printMatrix(int *arr)
{
    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 * 4 + j]);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.