存储另一个函数返回的**指针

Storing a **pointer returned by another function

我有一个 loadmap() 函数,基本上可以从文件中加载网格。在其中我动态创建了一个名为 'mapa'.

的整数数组
int loadmap(int *n) {
  int i, j;
  int **mapa;
  int num;
  char space;
  int count = 0;
  FILE *f;
  f = fopen(SUBOR, "r");

  fscanf(f, "%d%c", &num, &space);
  while (space != '\n') {
    count++;
    fscanf(f, "%d%c", &num, &space);
  }

  mapa = (int **)malloc(pocet * sizeof(int *));
  for (i = 0; i < pocet; i++) {
    mapa[i] = (int *)malloc(pocet * sizeof(int));
  }

  rewind(f);

  for (i = 0; i < pocet; i++) {
    for (j = 0; j < pocet; j++) {
      fscanf(f, "%d%c", &num, &space);
      mapa[i][j] = num;
    }
  }

  *n = count;

  return **mapa;
}

然后在主体中我想使用返回的指向数组的指针作为其他函数的参数。我尝试将它存储在我能想到的各种变量中,但没有任何效果。这是我现在拥有的。

int main() {
  int **mapa;
  int n;
  int ki, kj;

  **mapa = loadmap(&n);
  printf("rozmer stvorcovej mapy: %d\n", n);
  najdi_kopec(mapa, n, &ki, &kj);
  printf("suradnice kopca: %d, %d\n", ki, kj);
  zidi_kopec(mapa, n, ki, kj);

  return 0;
}

我认为这让我最接近解决方案,因为它适用于程序的其余部分需要编写的方式,但这会产生'未初始化的局部变量'mapa'使用'错误。我不确定我是否使用了正确的方法,我只是从 C 开始。您能否建议一些调整以使我的工作或其他工作方法完全可行?

谢谢

------------编辑------------

添加其他函数的声明


void najdi_kopec(int **mapa, int n, int *ki, int *kj) {
  int i, j;
  *ki = *kj = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (mapa[i][j] > mapa[*ki][*kj]) {
        *ki = i;
        *kj = j;
        // printf("i: %d, j: %d, mapa: %d\n", *ki, *kj, mapa[*ki][*kj]);
      }
}

void zidi_kopec(int **mapa, int n, int ki, int kj) {
  int i, j, max;
  while (ki != 0 && ki != n - 1 && kj != 0 && kj != n - 1) {
    max = 0;
    // sever
    if (ki - 1 >= 0 && mapa[ki - 1][kj] > max &&
        mapa[ki - 1][kj] < mapa[ki][kj]) {
      i = ki - 1;
      j = kj;
      max = mapa[i][j];
    }

    // vychod
    if (kj + 1 < n && mapa[ki][kj + 1] > max &&
        mapa[ki][kj + 1] < mapa[ki][kj]) {
      i = ki;
      j = kj + 1;
      max = mapa[i][j];
    }
    // juh
    if (ki + 1 < n && mapa[ki + 1][kj] > max &&
        mapa[ki + 1][kj] < mapa[ki][kj]) {
      i = ki + 1;
      j = kj;
      max = mapa[i][j];
    }
    // zapad
    if (kj - 1 >= 0 && mapa[ki][kj - 1] > max &&
        mapa[ki][kj - 1] < mapa[ki][kj]) {
      i = ki;
      j = kj - 1;
      max = mapa[i][j];
    }
    printf("i: %d, j: %d, vyska: %d\n", i, j, max);
    ki = i;
    kj = j;
  }
}

如果要return双指针mapa,loadmap()的声明应该是int ** loadmap(int *n),return值为mapa而不是 ** mapa.

在main函数中,你声明了一个双指针mapa,但是当你调用loadmap()函数时,你只是取mapa指向的第一个值。所以,如果你在另一个函数上使用 mapa 作为参数,mapa 没有被初始化,因为我们没有分配这个指针(例如使用 malloc)。

您的代码不正确。您必须在此处 post 其他功能。

更新:

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

int pocet = 5;
#define SUBOR "text.txt"
int ** loadmap(int *n) {
  int i, j;
  int **mapa;
  int num;
  char space;
  int count = 0;
  FILE *f;
  f = fopen(SUBOR, "r");
  if (!f) 
    exit(-1);
  fscanf(f, "%d%c", &num, &space);
  while (space != '\n') {
    count++;
    fscanf(f, "%d%c", &num, &space);
  }

  mapa = (int **)malloc(pocet * sizeof(int *));
  for (i = 0; i < pocet; i++) {
    mapa[i] = (int *)malloc(pocet * sizeof(int));
  }

  rewind(f);

  for (i = 0; i < pocet; i++) {
    for (j = 0; j < pocet; j++) {
      fscanf(f, "%d%c", &num, &space);
      mapa[i][j] = num;
    }
  }

  *n = count;

  return mapa;
}

void najdi_kopec(int **mapa, int n, int *ki, int *kj) {
  int i, j;
  *ki = *kj = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (mapa[i][j] > mapa[*ki][*kj]) {
        *ki = i;
        *kj = j;
        // printf("i: %d, j: %d, mapa: %d\n", *ki, *kj, mapa[*ki][*kj]);
      }
}

void zidi_kopec(int **mapa, int n, int ki, int kj) {
  int i, j, max;
  while (ki != 0 && ki != n - 1 && kj != 0 && kj != n - 1) {
    max = 0;
    // sever
    if (ki - 1 >= 0 && mapa[ki - 1][kj] > max &&
        mapa[ki - 1][kj] < mapa[ki][kj]) {
      i = ki - 1;
      j = kj;
      max = mapa[i][j];
    }

    // vychod
    if (kj + 1 < n && mapa[ki][kj + 1] > max &&
        mapa[ki][kj + 1] < mapa[ki][kj]) {
      i = ki;
      j = kj + 1;
      max = mapa[i][j];
    }
    // juh
    if (ki + 1 < n && mapa[ki + 1][kj] > max &&
        mapa[ki + 1][kj] < mapa[ki][kj]) {
      i = ki + 1;
      j = kj;
      max = mapa[i][j];
    }
    // zapad
    if (kj - 1 >= 0 && mapa[ki][kj - 1] > max &&
        mapa[ki][kj - 1] < mapa[ki][kj]) {
      i = ki;
      j = kj - 1;
      max = mapa[i][j];
    }
    printf("i: %d, j: %d, vyska: %d\n", i, j, max);
    ki = i;
    kj = j;
  }
}

int main() {
  int **mapa;
  int n;
  int ki, kj;

  mapa = loadmap(&n);
  printf("rozmer stvorcovej mapy: %d\n", n);
  najdi_kopec(mapa, n, &ki, &kj);
  printf("suradnice kopca: %d, %d\n", ki, kj);
  zidi_kopec(mapa, n, ki, kj);

  return 0;
}

text.txt 喜欢:

1 2 3 4 5 6
1 2 3 4 5

我测试的结果如下:

rozmer stvorcovej mapy: 5
suradnice kopca: 1, 0