I got an error trying to create my matrix. Error: Warning Passing argument 1 of " " from incompatible Pointer type

Asked

Viewed 43 times

0

I’m trying to create a code with a 3x3 matrix. However, I haven’t touched C for a while and I needed to review this content. I believe that the failure of the program is some question related to pointers, I have researched on the internet and I have not found anything that would help me to answer this question.

The error occurs in the function: build_matrix. Follow the code below.

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

 int existe(int valores[],int tamanho,int valor) 
 {
   int i;
   for( i=0;i<tamanho;i++){
     if(valores[i]==valor)
       return 1;
     }
    return 0;
   }

   int sorteio(int sorteados[],int tamanho){
   int numero,i;


   for(i=0;i<tamanho;i++){
    srand(time(NULL));
    numero=(rand()%9);//pega o resto da divisão do numero aleatorio por 9 (um numero menor q 9)
    while(existe(sorteados,i,numero)){
     numero=(rand()%9);
     }
     sorteados[i]=numero;
   }

 }

 void build_matriz( int ** matriz, int linha, int coluna){
int i,j;
int tamanho=9;
int sorteados[tamanho];
sorteio(sorteados,tamanho);
for(i=0;i<linha;i++){
    for(j=0;j<coluna;j++){
        sorteados[i+j]=matriz[i][j];
    }
}
}

 int show_matriz(int ** matriz,int linha, int coluna){
    for(int i=0;i<linha;i++){
     for(int j=0;j<coluna;j++){
       printf("+%d+",matriz[i][j]);
     }
     printf("\n");
    }

  }

int main(int argc, char *argv[ ]){
  int linha=3;
  int coluna=3;
  int mat[linha][coluna];
  build_matriz(mat,linha,coluna);
  show_matriz(mat,linha,coluna);


  }
  • it would be possible to place the number of the line where the error occurs, to facilitate the analysis ?

  • 1

    Here: drawn[i+j]=matrix[i][j]; should not be drawn[(i*line)+j]=matrix[i][j];? In the function there is you assume that the whole array is filled in but this is not necessarily true. Put srand(time(NULL)); out of the loop, so it runs only once.

  • Answering zenturix :In my Codeblocks the line is 33 but this may vary according to the spaces between each line, I’m not sure how many spaces I gave in this issue of the question.

  • Answering the Anonymity : You can elaborate more this detail that you say " Here: drawn[i+j]=matrix[i][j]; should not be drawn[(i*line)+j]=matrix[i][j];? ". I didn’t quite get that part.

  • 1

    Considering a 3x3 matrix with i+j you have numbers between 0 and 4 (since both vary between 0 and 2), even with duplicates (0+2 or 2+0) when it seems to me that you want to generate indices of a one-dimensional array between 0 and 8.

  • 1

    Yes, I was able to correct it. Do you want me to post the result ? I already modified all the work after you gave me this suggestion. Now I’m working on the same program but creating an 8(puzzle of 8) puzzle to exercise the content of my AI class.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.