Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing an empty 2d Array in C

Tags:

arrays

c

2d

I am writing a program that takes a 2D array and by use of a switch case, you can direct it to fill with random numbers and/or print the array among other things.

Before I fill the array with random numbers, I go to print the array just to see what it would do and I get this:

 176185448    1    1    01430232144
 32767180624652332767143023216832767
 143023216832767    0    11430232192
 32767176185344    1    0   14
 143023220832767    0    0    0
     0    0    0    0    0

This is my code for the print array function and I am passing plist from main:

 void PrintArray2D(int plist[M][N])
  {
  size_t row, column; //counter

    for (row = 0; row < M; ++row) 
    {
        for (column = 0; column < N; ++column) 
        {
            printf ("%5d" , plist[row][column]);
        }

        printf ("\n");
    }
}

My Program is working fine otherwise. When I fill with random numbers and then print, my output is correct. The array size is 6 by 5. I'm just curious as to why, anything is being printed at all when it is supposed to be an empty array. And even more curious as to the specific output I am getting.

like image 921
sujan Avatar asked Jun 22 '26 20:06

sujan


1 Answers

You are printing the value of uninitialized array. In this case the behavior of program is undefined. You may get any thing, either expected or unexpected result.
The value you are getting may be the some previous value stored at that location (called garbage value). Your program may gives erroneous result.

like image 93
haccks Avatar answered Jun 25 '26 10:06

haccks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!