c - 在 Xcode 上编译时出现 (lldb)

标签 c xcode4.5

我正在尝试用 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;
    }

但是它在我输入文件名后就停止了。在 Xcode 中,它显示为蓝色 (lldb) 和 0x7fff8ef27bcb:movb %dl, (%rsi)。我不知道这里出了什么问题。

提前致谢。

最佳答案

您还没有为您的 scanf 分配任何内存。您需要使用数组将字符串扫描到,而不是指针,或者动态分配内存(使用 malloc)给指针 nomeEntrada 和 nomeSaida。

基本上,您是在创建可以指向内存位置的变量,但您实际上并没有将任何内存放入其中,例如拥有尚未 build 的房屋的邮寄地址。

关于c - 在 Xcode 上编译时出现 (lldb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810766/

相关文章:

PHP 内部 : How does TSRMLS_FETCH Work?

view - 在 XCode 4.5 中正确使用新的 "container view"?

xcode - 无法从 xcode 中的提交中排除用户界面状态

twitter - 在 XCode 4.5 中找不到 MGTwitter : libxml/xmlreader. h 文件

c - 简单C题

c - 如何在 C 数组中找到下一个空间?

uiviewcontroller - 为什么我的 View Controller 不在窗口层次结构中?

Xcode 4.5 似乎仍然需要 @synthesize - 否则无法访问属性

c - 如何在版本控制中存储 C 库依赖项?

c - Maikefile错误make : *** [prepare] Error 127