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;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
anono
  • 25
  • 4
  • 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. – Some programmer dude Nov 04 '22 at 09:58
  • 2
    And please try to create a proper [mre], and [edit] your question to show it to us. – Some programmer dude Nov 04 '22 at 09:59
  • 1
    You should remove useless comments like `readMatrix(); //function to read matrix`. You already gave the function a self-documenting name. – Lundin Nov 04 '22 at 10:01
  • You can create the array in `main()` and pass a pointer to it in the function calls. – Ian Abbott Nov 04 '22 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? – anono Nov 04 '22 at 10:06

2 Answers2

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?

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 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 Nov 04 '22 at 10:14
  • 1
    @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](https://gustedt.gitlabpages.inria.fr/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. – Lundin Nov 04 '22 at 10:26
  • @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. – Ian Abbott Nov 04 '22 at 11:25
  • @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 Nov 04 '22 at 12:13
  • @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.) – Ian Abbott Nov 04 '22 at 15:19
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]);
        }
    }
}
lzl
  • 24
  • 1