0

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];
}
7
  • Please show more code, specifically, what calls this and the declarations of the variables passed in. This is not enough context. Commented Apr 10, 2014 at 16:07
  • Can you make a self-contained-working example so we can run it ? Commented Apr 10, 2014 at 16:07
  • I've just edited with all my code Commented Apr 10, 2014 at 16:08
  • 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. Does that even compile? Commented Apr 10, 2014 at 16:10
  • BTW: It's "vertices", not "verticies". Commented Apr 10, 2014 at 16:12

1 Answer 1

2

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.