How do I get rid of these warnings

Asked

Viewed 239 times

4

I’m doing a program that hunts a word from a matrix. For this I made 8 functions that sweep in all regions, as I need to return in the output the coordinates of the first and last letter.

For example, I need to find the word "home" in the matrix, the program returns the coordinates of the letter 'c' and the letter 'a' (last letter), as the entries I have and I need two values in the output.

I used pointers as parameters, only it is giving much Warning. Still missing to finish the complete code.

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

int Norte (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    j = *pc; k = 0;
    n = strlen(palavra);
    for (i = *pl; i < i+n; i++) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
    }
    *pl = i;
}

int Sul (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    j = *pc; k = 0;
    n = strlen(palavra);

    for (i = *pl; i < i+n; i++) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
    }
    *pl = i;
}

int Leste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    i = *pl; k = 0;
    n = strlen(palavra);

    for (j = *pc; j < j+n; j++) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
    }
    *pc = j;
}

int Oeste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    i = *pl; k = 0;
    n = strlen(palavra);

    for (j = *pc; j < j+n; j--) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
    }
    *pc = j;
}

int Nordeste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
     int i, j, n, k;
     k = 0;
     n = strlen(palavra);

     for (i = *pl, j = *pc; i < i+n; j++, i--) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
     }
     *pl = i; *pc = j;
}

int Noroeste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
     int i, j, n, k;
     k = 0;
     n = strlen(palavra);

     for (i = *pl, j = *pc; i < i+n; j--, i--) {
            if (cacapalavra [i][j] != palavra[k]) return -1;
            else k++;
     }
     *pl = i; *pc = j;
}

int Sudeste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
     int i, j, n, k;
     k = 0;
     n = strlen(palavra);

     for (i = *pl, j = *pc; i < i+n; j--, i++) {
             if (cacapalavra [i][j] != palavra[k]) return -1;
             else k++;
     }
     *pl = i; *pc = j;
}

int Sudoeste (char cacapalavra[][100], char palavra[], int *pc, int *pl) {
     int i, j, n, k;
     k = 0;
     n = strlen(palavra);

     for (i = *pl, j = *pc; i < i+n; j++, i++) {
             if (cacapalavra [i][j] != palavra[k]) return -1;
             else k++;
     }
     *pl = i; *pc = j;
}

int main () {

    char cacapalavra [100][100];
    char palavra [100];
    int i, j, k, n, *pc, *pl, aux;
    k = 0;

    scanf ("%d", &n);

    for (i = 0; i < n; i++)
          for (j = 0; j < n; j++)
                scanf ("%c", &cacapalavra[i][j]);

    scanf ("%s", palavra);

    for (i = 0; i < n; i++) {
          for (j = 0; j < n; j++) {
                if (palavra[k] == cacapalavra[i][j]) {
                *pc = j; *pl = i;
                aux = Norte(cacapalavra, palavra, &pc, &pl);
                aux = Sul(cacapalavra, palavra, &pc, &pl);
                aux = Leste(cacapalavra, palavra, &pc, &pl);
                aux = Oeste(cacapalavra, palavra, &pc, &pl);
                aux = Nordeste(cacapalavra, palavra, &pc, &pl);
                aux = Noroeste(cacapalavra, palavra, &pc, &pl);
                aux = Sudeste(cacapalavra, palavra, &pc, &pl);
                aux = Sudoeste(cacapalavra, palavra, &pc, &pl);
                }
          }
    }
}

Warnings:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\mdesousa\Documents\cacapalavra.c||In function 'main':|
C:\Users\mdesousa\Documents\cacapalavra.c|120|warning: passing argument 3 of 'Norte' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|5|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|120|warning: passing argument 4 of 'Norte' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|5|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|121|warning: passing argument 3 of 'Sul' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|17|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|121|warning: passing argument 4 of 'Sul' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|17|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|122|warning: passing argument 3 of 'Leste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|29|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|122|warning: passing argument 4 of 'Leste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|29|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|123|warning: passing argument 3 of 'Oeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|41|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|123|warning: passing argument 4 of 'Oeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|41|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|124|warning: passing argument 3 of 'Nordeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|53|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|124|warning: passing argument 4 of 'Nordeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|53|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|125|warning: passing argument 3 of 'Noroeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|65|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|125|warning: passing argument 4 of 'Noroeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|65|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|126|warning: passing argument 3 of 'Sudeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|77|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|126|warning: passing argument 4 of 'Sudeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|77|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|127|warning: passing argument 3 of 'Sudoeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|89|note: expected 'int *' but argument is of type 'int **'|
C:\Users\mdesousa\Documents\cacapalavra.c|127|warning: passing argument 4 of 'Sudoeste' from incompatible pointer type|
C:\Users\mdesousa\Documents\cacapalavra.c|89|note: expected 'int *' but argument is of type 'int **'|
||=== Build finished: 0 error(s), 16 warning(s) (0 minute(s), 0 second(s)) ===|
  • Which ones warnings? Where?

  • At the end "Warning: Passing argument 3 of 'Norte' from incompatible Pointer type|", I know it’s the pointer passage, but I don’t know how to get it out. I’m running on codeblocks

  • The edition had hidden them. There is a lot of error in this code, it will be dicífil find all. And the worst, that even though Compile, I will never trust him without making a thorough assessment. There are things I don’t even know how to fix, I’d have to understand what all the code does to solve. An example is that the function does not return anything in certain situations. It has to return, but what?

  • From what I understand the warnings are of passing pointer to function, all functions are very similar.

  • 1

    This is just a problem, this you solve by passing the variables without &. I’m talking about this at http://answall.com/q/125793/101 Part of the problem is the code is disorganized, some things are not easy to see.

  • Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site.

  • The error is time to pass the parameters, for example to the "North". This variable is already a pointer to "int", so you can’t put the "&" on pc in front, otherwise you’ll be interpreting the pointer as a pointer.

  • Try declaring: int **pc, if you want pointer pointer

Show 3 more comments

1 answer

4

I tidied up the variables that were being sent with & and are already pointers. I put returns in functions (kicked a value just to end Warning), made use of the variable aux and assigned values for variables pc and pl. I doubt you will do what you must, but I have solved the problem described in the question, if the question has more details I will improve. The code can be greatly improved.

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

int Norte(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    j = *pc; k = 0;
    n = strlen(palavra);
    for (i = *pl; i < i + n; i++) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pl = i;
    return 0;
}

int Sul(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    j = *pc; k = 0;
    n = strlen(palavra);
    for (i = *pl; i < i + n; i++) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pl = i;
    return 0;
}

int Leste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    i = *pl; k = 0;
    n = strlen(palavra);
    for (j = *pc; j < j + n; j++) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pc = j;
    return 0;
}

int Oeste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    i = *pl; k = 0;
    n = strlen(palavra);
    for (j = *pc; j < j + n; j--) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pc = j;
    return 0;
}

int Nordeste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    k = 0;
    n = strlen(palavra);
    for (i = *pl, j = *pc; i < i + n; j++, i--) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
     }
     *pl = i; *pc = j;
     return 0;
}

int Noroeste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
     int i, j, n, k;
     k = 0;
     n = strlen(palavra);
     for (i = *pl, j = *pc; i < i + n; j--, i--) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
     }
     *pl = i; *pc = j;
     return 0;
}

int Sudeste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    k = 0;
    n = strlen(palavra);
    for (i = *pl, j = *pc; i < i + n; j--, i++) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pl = i; *pc = j;
    return 0;
}

int Sudoeste(char cacapalavra[][100], char palavra[], int *pc, int *pl) {
    int i, j, n, k;
    k = 0;
    n = strlen(palavra);
    for (i = *pl, j = *pc; i < i + n; j++, i++) {
        if (cacapalavra[i][j] != palavra[k]) return -1;
        else k++;
    }
    *pl = i; *pc = j;
    return 0;
}

int main () {
    char cacapalavra [100][100];
    char palavra [100];
    int k = 0, n, *pc = 0, *pl = 0, aux;
    scanf ("%d", &n);
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%c", &cacapalavra[i][j]);
    scanf ("%s", palavra);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (palavra[k] == cacapalavra[i][j]) {
                *pc = j; *pl = i;
                aux = Norte(cacapalavra, palavra, pc, pl);
                aux = Sul(cacapalavra, palavra, pc, pl);
                aux = Leste(cacapalavra, palavra, pc, pl);
                aux = Oeste(cacapalavra, palavra, pc, pl);
                aux = Nordeste(cacapalavra, palavra, pc, pl);
                aux = Noroeste(cacapalavra, palavra, pc, pl);
                aux = Sudeste(cacapalavra, palavra, pc, pl);
                aux = Sudoeste(cacapalavra, palavra, pc, pl);
                printf("%d", aux); //só para eliminar o warning que não foi usada.
            }
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    I liked "See compiling in ideone", haha.

  • I’d like to know what’s wrong with the code to get negative. I know it’s not perfect, I said it. But I solved the problems posed in the question. Only the negative will not help anyone know what is wrong.

  • 1

    The problem is the assignment of the pointer. At the beginning it is stated that int *pc = 0, that is, he is NULL and then you do *pc = j, that is, it is de-referencing a pointer that is NULL. It must be full of error the program, but this is too obvious to pass up. The correct solution would be to remove the pointer and pass the address of the variable to the function;

  • 1

    Like I said, I just solved the problem of Warning As he asked, I warned that the code is still full of errors, not only that. The intention is not to do everything for him, but to solve what he asked. So much so that I didn’t even put that the code is working, only it is compiling. If he wants other help he can open other questions. The focus of this is the Warning. I didn’t introduce the bug, I just didn’t fix it.

  • 1

    The int *pc = 0 understood. In the second part I did not understand, I need the address at the position of the matrix to be updated, so I put *pc = j. What is leaving me more in doubt are these Warning, as for the other errors is no problem, I believe I can fix them without major problems.

Browser other questions tagged

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