0

I'm implementing a sliding puzzle program in C based on what the user inputs as their width of the slide (can only be 2, 3, 4). I feel like my syntax is correct for creating the 2D array (which is what we are using to represent the numbers in a later function), however, my function is not being called correctly. Instead it is hanging when I try to run it.

I cannot change the syntax of the function because it is a requirement that we have this in our program. So I'm wondering if it is a program passing in the function in main. I appreciate any help. Thank you.

int main() {                                                                    
int w; /*variable asking for width*/                                            

 printf("Width: ");                                                          
 scanf("%d\n", &w); /*&w will refer to make_tiles when function is called*/  
  make_tiles(w);                                                              
  return 0;                                                                   
}                                                                                                                                                            
int **make_tiles(int width) {                                                    
int **tiles;                                                                    
int counter = 0;                                                                
int i, j;                                                                       
tiles[i] = (int*) malloc(width*sizeof(int*));                               
for(i = 0; i < width; i++) {                                                    
for(j = 1; j < width; j++) {                                                
tiles[i][j] = counter++;                                                    
printf(" %d", tiles[i][j]);                                                       
}                                                                          
printf("\n");                                                               
}                                                                            
}             
  • 2
    change `scanf("%d\n", &w);` to `scanf("%d", &w);` – yano Dec 02 '17 at 06:34
  • `make_tiles()` has multiple counts of undefined behaviour in the statement `tiles[i] = (int *)malloc(width * sizeof (int *))` since (1) `tiles` itself is uninitialised (2) the `(int *)` conversion forces the code to compile, even though there is no `#include `. It doesn't help that the AMOUNT of memory being allocated is also incorrect. – Peter Dec 02 '17 at 06:43

1 Answers1

1

You have undefined behavior here. You have forget to allocate memory in tiles[i]. Also i is unintilized. Accessing an memory not allocated is also undefined behaviour.

int **tiles;                                                                    
int counter = 0;                                                                

tiles  = malloc(width*sizeof*tiles); 
if( tiles == NULL){
    fprintf(stderr,"Error in malloc");
    exit(1);
}                              
for(i = 0; i < width; i++) {
  tiles[i] = malloc(sizeof *tiles[i] * width);
  if( tiles[i] == NULL){
     fprintf(stderr,"Error in malloc");
     exit(1);
  }

}

You didn't return anything from the method. Also you should free the allocated memory.

If you notice this code you will see that I haven't cast the return value of malloc because this is unnecessary. Also it as mentioned in comment if you forget to include stdlib.h header. (Because prior to C11 undeclared functions are considered to return int which if differently size than an int* cause an erroneous result).

Example code:

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

int** make_tiles(int w);

int main() {
    int w; /*variable asking for width*/                                            

    printf("Width: ");                                                          
    if( scanf("%d", &w) != 1){
        fprintf(stderr, "Error in input");
        exit(1);
    }

    int **arr = make_tiles(w);  
    // you can work with arr here.   

    for(size_t i = 0; i < w ; i++)
        free(arr[i]);  
    free(arr);                                                       
    return 0;                                                                   
}                                                                                                                                        
int **make_tiles(int width) {                                                    
    int **tiles;                                                                    
    int counter = 0;                                                                

    if( width <= 0 ){
        fprintf(stderr,"%s\n","Error : Specify correct width [>=0]");
        exit(1);
    }
    tiles  = malloc(width*sizeof*tiles); 
    if( tiles == NULL){
        fprintf(stderr,"Error in malloc");
        exit(1);
    }                              
    for(size_t i = 0; i < width; i++) {
      tiles[i] = malloc(sizeof *tiles[i] * width);
      if( tiles[i] == NULL){
         fprintf(stderr,"Error in malloc");
         exit(1);
      }
    }  
    for(size_t i = 0; i < width ; i++){
        for(size_t j = 0; j < width ;  j++){
            tiles[i][j] = counter++;
            printf("%4d ", tiles[i][j]);

        }
        printf("%s","\n");
    }     
    return tiles;                                              
}       
user2736738
  • 30,591
  • 5
  • 42
  • 56