Question

I'm trying to implement Dijkstra's algorithm in C and I'm trying to pass a 2D array to a function. I tried compiling with both C99 and C11, so the way I wrote the function should (and does) compile. However, when I'm debugging I get the following error in the console after I've scanned in all my data and jump to my dijkstra() function, which does nothing at the moment. I'd appreciate if somebody could tell me what's going on.

If it's relevant at all, I'm using the GCC compiler, Eclipse Kepler, and Fedora 20

warning: Range for type <error type> has invalid bounds 0..-11105

#include <stdio.h>
#define INFINITY 4294967296

void dijkstra(int root, int end, int size, int adjMatrix[size][size]);
int main(void)
{
    /*------------------------------------------------------------------------*/
    /* Variable Declaration
     /*-----------------------------------------------------------------------*/
    int i, j; /* Temporary counting variables */
    int n; /* Number of verticies */
    int distance; /* Temporary distance variable */
    int root, end; /* Root and terminating verticies */

    /*------------------------------------------------------------------------*/
    /* Data Input
     /*-----------------------------------------------------------------------*/
    /*Get number of verticies and check for erroneous input */
    printf("Please enter the number of verticies in your graph: ");
    scanf("%d", &n);
    while (n <= 0)
    {
        printf("\nERROR: value must not be less than or equal to zero! \n\n"
               "Please enter the number of verticies in your graph: ");
        scanf("%d", &n);
    }

    /*Get root node and check for erroneous input */
    printf("Please enter the root vertex: ");
    scanf("%d", &root);
    while (root > n || root <= 0)
    {
        printf("\nERROR: value must be less than or equal to the total number"
                " of\n verticies and may not be less than or equal to "
                "zero! \n\nPlease enter the root vertex: ");
        scanf("%d", &root);
    }

    /*Get terminating node and check for erroneous input */
    printf("Please enter the terminating vertex: ");
    scanf("%d", &end);
    while (end > n || end <= 0)
    {
        printf("\nERROR: value must be less than or equal to the total number"
                " of\n verticies and may not be less than or equal to "
                "zero! \n\nPlease enter the terminating vertex: ");
        scanf("%d", &end);
    }

    /* Fill adjacency matrix */
    printf("\n\nPlease enter the distance between verticies i and j below.\nIf "
            "the two verticies are disjoint, enter a negative number "
            "instead.\n");

    int adjMatrix[n][n];
    /* Scan across entire array  */
    for (i = 0; i < n; ++i)
    {
        /* Scan across only half of array; no need to ask user for the same
         * distance twice.
         */
        for (j = 0; j < i; ++j)
        {
            printf("\ndistance(%d,%d) = ", i + 1, j + 1);
            scanf("%f", &distance);
            /* Distance from i to j is the same as the distance from j to i.
             * Hence we place it in two parts of the adjacency matrix.
             */
            adjMatrix[i][j] = distance;
            adjMatrix[j][i] = distance;
        }
        /*For the case of i = j, set distance to -1 as self-loops are not
         * taken into consideration here.
         */
        adjMatrix[j][i] = -1;
    }

    dijkstra(root,end,adjMatrix,n);

    return 0;
}

void dijkstra(int root, int end, int size, int adjMatrix[size][size])
{
    int i = adjMatrix[3][1];
}
Was it helpful?

Solution

The function takes the matrix last, and the size next-to-last, but you call it with the size and matrix the other way around.

This causes undefined behavior when the function uses the size as a pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top