0

While compiling the below code, I am getting warning:

warning: assignment from incompatible pointer type
maze->mazeValue = mazeValue;

This is the maze solving code. Have tried it but couldn't identify the issue. If I change
char mazeValue[BUFFERSIZE][BUFFERSIZE] to char \*\* mazeValue**, the program gets compiled the did not execute. Windows throws alert.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFFERSIZE (1000)
#define MAZE_ENTRANCE '.'


struct maze {
    char ** mazeValue;
    int startx, starty;
    int numrows;
    int initdir;
};

void ReadMaze(struct maze * maze);

int main(int argc, char *argv[]) {
    struct maze maze;


    ReadMaze(&maze);
    printf("Thanks");
    //PrintStage(&maze);

    return EXIT_SUCCESS;
}


/*  Creates a maze from a file  */

void ReadMaze(struct maze * maze) {
    char buffer[BUFFERSIZE];
    char  mazeValue[BUFFERSIZE][BUFFERSIZE];
    //char workingMaze [BUFFERSIZE][BUFFERSIZE];
    //char ** map;
    int rows = 0, foundentrance = 0, foundexit = 0;
    int columns = 0, i = 0;

    /*  Determine number of rows in maze  */


    while ( fgets(buffer, BUFFERSIZE, stdin) ){

       //puts(buffer);
       columns = strlen(buffer);

       /*
       for(i=0; buffer[i] != '\0'; i++)
       {
            //printf("Row: %d\n", rows);
            //putchar(buffer[i]);
            //printf("\n");
            mazeValue[rows][i] = buffer[i];
            //putchar(mazeValue[rows][i]);
            //printf("\n");
       }
       */

       strcpy(mazeValue[rows], buffer);

       for ( i = strlen(mazeValue[rows]) - 1; isspace(mazeValue[rows][i]); --i )
            mazeValue[rows][i] = 0;

        /*  Check for entrance and save the location if it finds  */

        if ( !foundentrance && rows == 0) {
            i = 0;

            printf("rows %d\n", rows );
            printf("i %d\n", i );

            while ( mazeValue[rows][i] != MAZE_ENTRANCE && mazeValue[rows][i++] ){

                if ( mazeValue[rows][i] == MAZE_ENTRANCE ) {
                    maze->startx = i;
                    maze->starty = rows;
                    foundentrance = 1;
                }
            }
        }
        ++rows;
    }

    maze->mazeValue = mazeValue;
    maze->numrows = rows;

    printf("maze->startx %d\n", maze->startx);
    printf("maze->starty %d\n", maze->starty );

    printf("\n");
    printf("Stage 1\n");
    printf("=======\n");
    printf("maze has %d rows and %d columns\n\n", rows, columns);

    i=0;
    int j;

    for(i=0; i<=rows; ++i)
    {
        for(j=0; j<=columns; ++j){
            printf("%c", mazeValue[i][j]);
            printf("%c", mazeValue[i][j]);
        }
        printf("\n");
    }

    printf("\n");
    printf("foundentrance: %d\n", foundentrance);

}
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27

2 Answers2

2

Even if the assignment worked, you can't do that. You're trying to store a local array, and use it later, which is not permitted and will give you undefined behavior.

You need to heap-allocate (using malloc()) the data inside the structure, not use a local array.

First change the structure member to be a plain character pointer:

char *mazeValue;  /* Single-star! */

Then allocate it like:

maze->mazeValue = malloc(BUFFERSIZE * BUFFERSIZE);

Then do the 2D-indexing yourself, it's much easier:

maze->mazeValue[y * BUFFERSIZE + x] = '?';

Of course x and y must both be between 0 and BUFFERSIZE - 1.

Note: if you really want BUFFERSIZE to be constant, you can instead declare the array directly in the structure, going back to a 2D-array:

char mazeValue[BUFFERSIZE][BUFFERSIZE];
unwind
  • 391,730
  • 64
  • 469
  • 606
1

The pointers are of different types. The mazevalue in the struct is a pointer to a pointer.

The mazevalue in the ReadMaze function is a 2D array. A pointer to a 2D array has a type of pointer to array and is declared like this int (*mazevalue)[BUFFERSIZE].

However, changing the pointer in the struct will not solve your problem. The mazevalue in ReadMaze is a local variable and can be overwritten when the function exits. The value in the structure may be overwritten in this case.

The best option would be to keep the struct the same, and change the ReadMaze function. In this you should have mazevalue as a pointer to pointer. Then you can malloc a 2D array as required.

e.g.

void ReadMaze(struct maze * maze) {
   char  **mazeValue;

   mazevalue = malloc(BUFFERSIZE * sizeof(char*));
   for (i=0; i<BUFFERSIZE; i++)
   {
      mazevalue[i] = malloc(BUFFERSIZE);
   }
   // Other code below...

}

You can also optimize and allocate only the required no of rows and columns required for the application.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Thanks a lot. After implementing above code, now it's not giving any error. But while printing maze, it is printing few junk characters.
    ── ;; pp→→;;
    – user6344678 May 19 '16 at 09:12