Segfault in an unusual place
<p>I was writing some code to make a .pgm image for my class, and when I ran it for the first time, I got a segfault. I kinda expected that, because I'm notoriously bad about calling a member of a struct that was initialized to NULL. But when I put my debug statements in (just printf functions with numbers), it gave me a segfault well before I expected.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

typedef struct pgm {
    int rows;
    int cols;
    int **pixels;
} pgmPic; 

void dataCreate(pgmPic *);
void trailCreate(pgmPic *);
pgmPic *blaze(pgmPic *, char*);

int main(int argc, char** argv){
    printf("Hi\n");
    if(argc != 3){
        printf("Improper use of arguments.\n");
        return 0;
    }
    int row, col;
    printf("point 0");
    FILE *data = fopen(argv[1], "r");
    printf("point 1");
    fscanf(data, "%d\n%d\n", &amp;row, &amp;col);
    printf("point 2");
    pgmPic *myPic = (pgmPic *) malloc(sizeof(pgmPic));
    myPic-&gt;pixels = (int**) malloc(sizeof(int)* myPic-&gt;rows);
    for(int a = 0; a &lt; myPic-&gt;rows; a++)
        myPic-&gt;pixels[a] = (int*) malloc(sizeof(int) * myPic-&gt;cols);
    printf("point 3");
    int elev[row][col];
    for(int w = 0; w &lt; row; w++){
        for(int v = 0; v &lt; col; v++){
            fscanf(data, "%d", &amp;elev[w][v]);
        }
    }
    printf("point 4");
    int min = elev[0][0];
    int max = elev[0][0];
    for(int x = 0; x &lt; row; x++){
        for(int y = 0; y &lt; col; y++){
            if(elev[x][y] &lt; min)
                min = elev[x][y];
            if(elev[x][y] &gt; max)
                max = elev[x][y];
        }
    }
    printf("point 5");
    double multiplier = (max-min)/200;
    for(int w = 0; w &lt; row; w++){
        for(int v = 0; v &lt; col; v++){
            elev[w][v] = elev[w][v] - min;
            elev[w][v] = elev[w][v] * multiplier;
            myPic-&gt;pixels[w][v] = 255 - elev[w][v];
        }
    }
    printf("point 6");
    myPic-&gt;rows = row;
    myPic-&gt;cols = col;
    printf("point 7");
    dataCreate(myPic);
    printf("point 8");
    myPic = blaze(myPic, argv[2]);
    printf("point 9");
    trailCreate(myPic);
    return 0;
}

void dataCreate(pgmPic *pic){
    FILE *myfile = fopen("data.pgm", "w");
    fprintf(myfile, "P2\n");
    fprintf(myfile, "%d %d\n", pic-&gt;cols, pic-&gt;rows);
    for(int i = 0; i &lt; pic-&gt;rows; i++){
        for(int j = 0; j &lt; pic-&gt;cols; j++){
            fprintf(myfile, "%3d ", pic-&gt;pixels[pic-&gt;rows][pic-&gt;cols]);
        }
        fprintf(myfile, "\n");
    }
    fclose(myfile);
} 

void trailCreate(pgmPic *pic){
    FILE *myfile = fopen("data-trail.pgm", "w");
    fprintf(myfile, "P2\n");
    fprintf(myfile, "%d %d\n", pic-&gt;cols, pic-&gt;rows);
    for(int i = 0; i &lt; pic-&gt;rows; i++){
        for(int j = 0; j &lt; pic-&gt;cols; j++){
            fprintf(myfile, "%3d ", pic-&gt;pixels[pic-&gt;rows][pic-&gt;cols]);
        }
        fprintf(myfile, "\n");
    } 
    fclose(myfile);
}

pgmPic *blaze(pgmPic *pic, char* dir){
    pgmPic *thing = pic;
    int row, col;
    if(strcmp(dir, "W-E") == 0){
        row = pic-&gt;rows / 2;
        col = 0;
        while(col &lt; pic-&gt;cols){
            thing-&gt;pixels[row][col] = 0;
            col++;
            if(thing-&gt;pixels[row-1][col] &lt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col] &gt;= thing-&gt;pixels[row+1][col])
                continue;
            if(thing-&gt;pixels[row-1][col] &gt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row-1][col] &gt;= thing-&gt;pixels[row+1][col]){
                row--;
                continue;
            }
            if(thing-&gt;pixels[row-1][col] &lt;= thing-&gt;pixels[row+1][col] &amp;&amp; thing-&gt;pixels[row][col] &lt;= thing-&gt;pixels[row+1][col]){
                row++;
                continue;
            }
        }
    }
    if(strcmp(dir, "E-W") == 0){
        row = pic-&gt;rows / 2;
        col = pic-&gt;cols - 1;
        while(col &gt; 0){
            thing-&gt;pixels[row][col] = 0;
            col--;
            if(thing-&gt;pixels[row-1][col] &lt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col] &gt;= thing-&gt;pixels[row+1][col])
                continue;
            if(thing-&gt;pixels[row-1][col] &lt;= thing-&gt;pixels[row+1][col] &amp;&amp; thing-&gt;pixels[row][col] &lt;= thing-&gt;pixels[row+1][col]){
                row++;
                continue;
            }
            if(thing-&gt;pixels[row-1][col] &gt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row-1][col] &gt;= thing-&gt;pixels[row+1][col]){
                row--;
                continue;
            }
        }
    }
    if(strcmp(dir, "S-N") == 0){
        col = pic-&gt;cols / 2;
        row = pic-&gt;rows - 1;
        while(col &gt; 0){
            thing-&gt;pixels[row][col] = 0;
            row--;
            if(thing-&gt;pixels[row][col-1] &lt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col] &gt;= thing-&gt;pixels[row][col+1])
                continue;
            if(thing-&gt;pixels[row][col-1] &gt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col-1] &gt;= thing-&gt;pixels[row][col+1]){
                row--;
                continue;
            }
            if(thing-&gt;pixels[row][col-1] &lt;= thing-&gt;pixels[row][col+1] &amp;&amp; thing-&gt;pixels[row][col] &lt;= thing-&gt;pixels[row][col+1]){
                row++;
                continue;
            }
        }
    }
    if(strcmp(dir, "N-S") == 0){
        col = pic-&gt;cols / 2;
        row = 0;
        while(col &lt; pic-&gt;cols){
            thing-&gt;pixels[row][col] = 0;
            row++;
            if(thing-&gt;pixels[row][col-1] &lt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col] &gt;= thing-&gt;pixels[row][col+1])
                continue;
            if(thing-&gt;pixels[row][col-1] &lt;= thing-&gt;pixels[row][col+1] &amp;&amp; thing-&gt;pixels[row][col] &lt;= thing-&gt;pixels[row][col+1]){
                col++;
                continue;
            }
            if(thing-&gt;pixels[row][col-1] &gt;= thing-&gt;pixels[row][col] &amp;&amp; thing-&gt;pixels[row][col-1] &gt;= thing-&gt;pixels[row][col+1]){
                col--;
                continue;
            }
        }
    }
    return thing;
}
</code></pre>

<p>Now when I ran this, I was expecting the segfault to be somewhere after point 2 or 3, but when I ran it, I got a segfault before I got to point 0. Do you know what's causing that?</p>