Aviral Gupta
Aviral Gupta

Reputation: 11

warning: prod may be used uninitialized in this function

im getting started off with C... i ran this code and im getting the error "warning: prod may be used uninitialized in this function" please help! i was given an array and had to find the product of all integers within the starting cordinates and ending cordinates.

#include <stdio.h>

int main()

{
    printf("start\n");

    int array[100][100], N, M, Q, queries[100][4], i, j, k;
    long long int prod;
    scanf("%d %d", &N, &M);
    for (i=0; i<N; i++)
    {
        for (j=0; j<M; j++)
        {
            scanf("%d",&array[i][j]);
        }
    }

    scanf("%d",&Q);
    for (i=0; i<Q; i++)
    {
        //queries[i] = {x1, y1, x2, y2}
        for (j=0; j<4; j++) scanf("%d",&queries[i][j]);
        prod = 1;

        for (j=queries[i][0]; j<=queries[i][2]; j++)
        {
            for (k=queries[i][1]; k<=queries[i][3]; k++)
            {
                prod *= array[j][k];
                prod %= 1000000007;
            }
        }
    }

    return prod;
}

Upvotes: 1

Views: 57

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754560

If Q is entered as 0 or a negative number, return prod; at the end will be returning an uninitialized value. So, the warning is correct. Trust your compiler!

Note that on Unix-like (POSIX-based) systems, the value returned to the calling program (probably a shell) will be in the range 0..255; the effective value returned will be prod % 256. Also, only the last of the Q series of calculations will be used — there's a lot of extraneous work being done, including making the user type values that will be ignored, which isn't kind.

Upvotes: 2

Related Questions