3

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.

0

3 Answers 3

2

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.

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

2 Comments

Oh! Awesome. I just initialized it. Thanks! Just wondering though, where are those random numbers coming from?
May be from other programs executed previously and written some data on those locations.
2

Are you initialising the array?

If not, you are more than likely getting the remains of whatever was in memory beforehand.

To initialise it to all zeros quickly, wherever it is defined have something like

int list[M][N] = {0};

Just a warning, the zero does not mean set all values to 0. It sets the first elements to the contents of the curly braces. So:

int values[M] = {1,2,3};

Would set the first three numbers to 1,2, and 3 and the remainder to zeros.

Comments

2

Variables in C must be initialized. If you don't initialize a variable (like an array) it contains some non-logic numbers (like address of a memory unit - the value which had been there before in that location of the memory) The thing you're printing is that! you can't do this in some languages like Java, if you do so, you get an compilation error. I hope this helps

2 Comments

Yes, That was my problem. I didn't initialize the array. Oh okay, that makes sense as to what it was printing. Thanks for your help.
@user3261213 You're welcome buddy. You can verify my answer by clicking on the tick next to it. :)

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.