TransWikia.com

Warnings in gcc compiler

Stack Overflow Asked by Hygor Marques on December 3, 2020

I’m gonna make a program that convolves a matrix, but first I need to create this matrix and load it with random numbers, but in my methods to do this I received warnings and I cannot resolve this because I am newbie in this and this is gonna make me crazy

#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 5

// Create a Matrix
int **generateMatrix(int row, int col){
  int **mat;
  mat=(int**)malloc(sizeof(int*)*row);
  for(int i=0;i<row;i++){
    mat[i]=(int*)malloc(sizeof(int)*col);
  }
  if(mat==NULL){
    printf("Insufficient Memory...");
    return 0;
  }
  return mat;
}

// Load a Matrix
int loadMatrix(int *x[][COL], int row, int col){
  int i;
  for(int r=0;r<row;r++){
      for(int c=0;c<col;c++){
        x[r][c]=rand()%256;
    }
  }
  return x;
}

// Print a Matrix
void printMatrix(int *x[][COL], int row, int col){
  for(int r=0;r<row;r++){
    for(int c=0;c<col;c++){
      printf("t%d", x[r][c]);
    }
    printf("n");
  }
}

int main(){
  int **matrix, **kernel;

  // Create the Matrix
  matriz=generateMatrix(ROW, COL);
  kernel=generateMatrix(3,3);

  // Load the Matrix
  loadMatrix(matrix, ROW, COL);
  loadMatrix(kernel, 3, 3);

  // Print Matrix
  printf("Matrixn");
  printMatrix(matrix, ROW, COL);
  printf("n");

  printf("Kerneln");
  printMatrix(kernel, 3, 3);
  printf("n");

  free(matrix);
  free(kernel);
}

The warnings I received are bellow

main.c:25:20: warning: assignment makes pointer from integer without a cast [-Wint-conversion]                                  
main.c:28:14: warning: return makes integer from pointer without a cast [-Wint-conversion]                                      
main.c:35:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]                  
main.c:49:18: warning: passing argument 1 of ‘loadMatrix’ from incompatible pointer type [-Wincompatible-pointer-types]         
main.c:21:9: note: expected ‘int * (*)[5]’ but argument is of type ‘int **’                                                     
main.c:50:18: warning: passing argument 1 of ‘loadMatrix’ from incompatible pointer type [-Wincompatible-pointer-types]         
main.c:21:9: note: expected ‘int * (*)[5]’ but argument is of type ‘int **’                                                     
main.c:54:19: warning: passing argument 1 of ‘printMatrix’ from incompatible pointer type [-Wincompatible-pointer-types]        
main.c:32:10: note: expected ‘int * (*)[5]’ but argument is of type ‘int **’                                                    
main.c:58:19: warning: passing argument 1 of ‘printMatrix’ from incompatible pointer type [-Wincompatible-pointer-types]        
main.c:32:10: note: expected ‘int * (*)[5]’ but argument is of type ‘int **’ 

One Answer

I believe you must think a bit on how do you want to build the matrix thing.

Let's say we are talking about a 5x5 array of int

  • Plan A: int kernel[][]

We will have a starting address kernel and 5 x 5 x sizeof(int) values, let's say 4 bytes to a total of 100 bytes using a 32-bits compiler. This way you would declare as int kernel[5][5]. And kernel[0] is int[5]

  • Plan B: int** kernel

We will have kernel as a pointer to an array of pointers to int, starting at *kernel. It is another animal. kernel points to an area that has pointers to int. Let's say we have also 5 of them. kernel then points to an area of 5 pointers to int. Not an array of int. Just 5 int. In order to each one point to an array of 5 int we should have declared kernel as int* kernel[5]; and in this case *kernel[0] up to *kernel[4] would indeed point to an array of 5 int.

But note that we have no way of knowing how many pointers are in *kernel. This is the reason why main() is declared int main(int argc, char** argv);: someone must tell the program how many pointers are in *argv.

Back to your code:

You must use this knowledge in generateMatriz() and loadMatriz().

To use array notation the last dimension must be constant. But see your prototype for loadMatriz()

int loadMatriz(int* x[][COL], int row, int col);

what is x here? It is not Plan A. It is not plan B. x is and array of pointers to int. Each element of x is a pointer to int. And it is not what you passed on main():

int main() {
    int** matriz, ** kernel;

    // Create the Matriz
    matriz = generateMatriz(ROW, COL);
    kernel = generateMatriz(3, 3);

    // Load the Matriz
    loadMatriz(matriz, ROW, COL);
    loadMatriz(kernel, 3, 3);

kernel is int**. It is not int*[][]. Think about this. This is the reason of the compiler errors: you must build the matrix according to plan ;)

Correct answer by arfneto on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP