0

I'm trying to compile this code in C:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100

void MARCAR(int L[MAX][MAX], int n, int m, int queijo_i, int queijo_j);
void GRAVAR(int L[MAX][MAX], int n, int m, int rato_i, int rato_j, int cam[], int   queijo_i, int queijo_j);
void push(int obj, int stack[], int *topo, int n, int m);
int pop(int stack[], int *topo);

void push(int obj, int stack[], int *topo, int n, int m)
{
    if(*topo + 1 < n * m)
    {
        stack[*topo] = obj;
        *topo = *topo + 1;
        printf("%i foi empilhado com sucesso", obj);
    }
    else
        printf("Stack Overflow");
}

int pop(int stack[], int *topo)
{
    int obj;
    if(*topo >=0)
    {
        obj = stack[*topo];
        *topo = *topo - 1;
        printf("%i foi desempilhado com sucesso", obj);
        return obj;
    }
    else
        printf("Stack Underflow");
    return -2;
}

void MARCAR(int L[MAX][MAX], int n, int m, int queijo_i, int queijo_j)
{
    int i, j;/*coordenadas da matriz*/
    int stack[MAX * MAX];
    int topo;
    int k;/*contador*/

    topo = 0;
    i = queijo_i;
    j = queijo_j;

    L[i][j] = 1;
    push(i, stack, &topo, n, m);
    push(j, stack, &topo, n, m);

    while(i < n && j < m)
    {
        k = 0;
        if(L[i][j + 1] == 0 && j < n)
        {
            push(i, stack, &topo, n, m);
            push(j + 1, stack, &topo, n, m);
            L[i][j + 1] = L[i][j] + 1;
        }
        else
            k++;
        if(L[i][j - 1] == 0 && j - 1 > 0)
        {
            push(i, stack, &topo, n, m);
            push(j - 1, stack, &topo, n, m);
            L[i][j + 1] = L[i][j] + 1;
        }
        else
            k++;
        if(L[i + 1][j] == 0 && i + 1 < m)
        {
            push(i + 1, stack, &topo, n, m);
            push(j, stack, &topo, n, m);
            L[i][j + 1] = L[i][j] + 1;
        }
        else
            k++;
        if(L[i - 1][j] == 0 && i - 1 > 0)
        {
            push(i - 1, stack, &topo, n, m);
            push(j, stack, &topo, n, m);
            L[i][j + 1] = L[i][j] + 1;
        }
        else
            k++;
        if(k >= 4)
            break;
        j = pop(stack, &topo);
        i = pop(stack, &topo);
        }

}

void GRAVAR(int L[MAX][MAX], int n, int m, int rato_i, int rato_j, int cam[], int queijo_i, int queijo_j)
{
    int k;/*contador*/
    int i, j; /*coordenadas da matriz L*/
    int l;/*cocomprimento do caminho*/

    k = 0;
    l = L[rato_i][rato_j];

    cam[0] = rato_i;
    cam[1] = rato_j;

   while( !(cam[k] == queijo_i && cam[k + 1] == queijo_j) && k < (n - 1) * (m - 1) )/*enquanto o rato nao chega no queijo ou o comprimento do caminho nao ultrapassa o tamanho da matrix*/
   {
       k++;
       i = cam[k];
       j = cam[k + 1];
       /*analisar as vizinhancas de (i, j)*/
       if( (L[i][j + 1] = l - 1) && (j + 1 < n) )
       {
           cam[k] = i;
           cam[k + 1] = j + 1;
       }
       if( (L[i][j - 1] = l - 1) && (j - 1 >= 0) )
       {
           cam[k] = i;
           cam[k + 1] = j - 1;
       }
      if( (L[i + 1][j] = l - 1) && (i + 1 < m) )
      {
          cam[k] = i + 1;
          cam[k + 1] = j;
      }
      if( (L[i - 1][j] = l - 1) && (i - 1 >= 0) )
      {
          cam[k] = i;
          cam[k + 1] = j + 1;
      }
      l = l - 1;
  }
}

int main(int argc, char *argv[])
{
        int i, j;/*coordenadas do labirinto*/
        int m, n; /*dimensoes da matriz*/
        int L[MAX][MAX];/*labirinto*/
        int cam[MAX * MAX]; /*menor caminho*/
        char *nomeEntrada;
        char *nomeSaida;
        FILE *arqEntrada;/*arquivo de entrada*/
        FILE *arqSaida;/*arquivo de saida*/
        int k;/*contador*/
        int rato_i;
        int rato_j;
        int queijo_i;
        int queijo_j;

        k = 0;

        if(argc != 3)/*não possui dois argumentos na linha de comando*/
        {
            printf("Digitar o nome do arquivo de entrada e de saida como parâmetros!\n");
            scanf("%s %s", nomeEntrada, nomeSaida);
            printf("Use o programa assim: %s arquivo_entrada arquivo_saida\n",argv[0]);
            exit(0);
        }
        else
        {
            nomeEntrada = argv[1];
            nomeSaida = argv[2];
        }

        if( (arqEntrada = fopen(nomeEntrada, "r")) != NULL) /*testar se o arquivo abre corretamente!*/
        {
            /*montagem do labirinto na matriz atraves de ENTRADA.txt*/
            while(!feof(arqEntrada))
            {
                fscanf(arqEntrada,"%i_%i %i", &i, &j, &L[i][j]);
                switch(k)
                {
                    case 1:
                        rato_i = i;
                        rato_j = j;
                        break;

                    case 2:
                        queijo_i = i;
                        queijo_j = j;
                        break;

                    default:
                        break;
                }
                n = i;
                m = j;
                k++;
            }
            fclose(arqEntrada);
            MARCAR(L, n, m, queijo_i, queijo_j);
        }


        if( (arqSaida = fopen(nomeSaida, "w")) != NULL)
        {
            if(L[rato_i][rato_j] != 0)
            {
                GRAVAR(L, n, m, rato_i, rato_j, cam, queijo_i, queijo_j);
                for(i = 0; i < n*m; i++)
                {
                    fprintf(arqSaida, "( %i , %i ) \t", cam[i], cam[i + 1]);
                }
            }
            else
            {
                printf("Caminho nao existe");
                fprintf(arqSaida, "Caminho nao existe");
            }

            fclose(arqSaida);
        }

        return 0;
    }

However it stops just after I put the names of the files. In Xcode it appears in blue (lldb) and 0x7fff8ef27bcb: movb %dl, (%rsi). I don't know what's wrong here.

Thanks in advance.

1 Answer 1

2

You haven't allocated any memory for your scanf. You need to use an array to scan a string into, not a pointer, or dynamically allocate memory (with malloc) to the pointers nomeEntrada and nomeSaida.

Basically you are creating variables that can point to a memory location, but you haven't actually put any memory into it, like having a mailing address to a house that hasn't been built yet.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but I'm confused. Could you, please, provide an example?
sure. Instead of char * nomeEntrada; use char nomeEntrada[80]; alternatively, leave it as char * nomeEntrada but say char * nomeEntrada = (char *)malloc(sizeof(char) * 80);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.