dpalacios
dpalacios

Reputation: 21

Not able to retrieve or store 2d array positions in If statement

I am attempting to solve this maze problem, however, what I have now represents a maze txt file that when ran it creates the dimensions (Rows and Columns) and prints out the matrix where "b" is soppose to be as well as the spaces and "s" for 'Starting point'

I am trying to get the starting coordinates where the 's' is at in this matrix.

Here is how the maze.txt file looks like:

bbbb bbbbb
bb     bbb
b bb b bbb
bb   b b b
bb bbb  bb
bb     b b
bb b b bbb
bb    s bb
bbbbbbbbbb



Here is my function to create the matrix:

 void CreateGrid(FILE* fp, int rowCount, int colNum)
    {
    int i, j;
   int startPos[2];

     int dimensions[rowCount][colNum];

        for (i = 0; i < rowCount; i++) {
             for (j = 0; j < colNum; j++) {
                    char brick = getc(fp);

                    if (brick == "s"){
                       startPos[0] = i;
                       startPos[1] = j;

                    }
                     dimensions[i][j] = brick;


                    printf("%c", dimensions[i][j]);
        }



    }
    printf("\n%d %d", startPos[0], startPos[1]);



Expected Result --> x: 7, y: 7


Actual Result --> 0 0

My logic was that since it scans through each character by row and column, it could indicate where the letter 's' will be and from that I would be able to retrieve its current position in the loop but it appears to be not the case.

I am not sure if this is memory allocation error but I am a bit new to C so this is all a bit confusing for me because I know this would work in a higher language.

Upvotes: 0

Views: 40

Answers (1)

Girocired
Girocired

Reputation: 57

I am not sure how the compiler didn't return an error, but try putting single quotes in if statement:

Use if (brick == 's'){ Instead of if (brick == "s"){

Double quotes are strings, so here you are comparing pointer to "s" with character on that is saved in "brick" variable.

The value you are printing is unintialized due to the fact that if will never execute and 0 is the most likely case.

I would recommend using -Wall compiler option while writing/debugging code as it would, for this case for example output warning: comparison between pointer and integer [enabled by default]

Upvotes: 1

Related Questions