0

我在理解如何编写解决以下问题的代码时遇到问题:我有一个包含二维数组的结构。然后我有一个递归函数,它将指向结构的指针作为参数,我希望递归函数能够操作发送的结构,而不是本地副本。

该结构在函数 initStruct 中初始化,其中为 2D 数组分配内存。递归函数构建一个数组,并在特定点调用一个函数将其插入到结构的数组中。

编码:

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

int** spBasis(int);
void mpBasis(int**, int, int, int, int, int, int, int*, struct mpBasis *, int, int);
void initMpBasis(struct mpBasis *, int, int);
void insertMpState(struct mpBasis *, int *);

struct mpBasis {
    int** basis;
    int size;
    int capacity;
};

int main() {
    int a, b, c, d;
    char maxE[256];
    char noParticles[256];
    char P[256];
    char M[256];
    FILE *fp;
    int **spStates;
    struct mpBasis *mp;
    int mpState[6] = {0, 0, 0, 0, 0, 0};

    printf("Input max e for sp states, no of particles, parity (1 for odd and 0 for even) and magnetic projection: ");

    gets(maxE);
    gets(noParticles);
    gets(P);
    gets(M);
    spStates = spBasis(atoi(maxE));
    fp = fopen("spStates.txt", "a+");
    fprintf(fp, "E\tj\tl\tm\n");
    for (a = 0; a < 330; a++) {
        fprintf(fp, "State %d: ", a+1);
        for (b = 0; b < 4; b++) {
            fprintf(fp, "%d\t", spStates[a][b]);
        }
        fprintf(fp, "\n");
    }

    mp = malloc(sizeof(struct mpBasis));
    initMpBasis(mp, 5449, 6);

    for (c = 0; c < 5449; c++) {
        for (d = 0; d < 6; d++) {
            fprintf(fp, "%d: %d\t", c, mp->basis[c][d]);
        }
        fprintf(fp, "\n");
    }

    printf("%p\n", (void*) mp);

    printf("hello 3");
    mpBasis(spStates, 0, atoi(maxE), 0, atoi(M), 0, atoi(P), mpState, mp, 0, 0); 

    fclose(fp);
    return 0;
}

int** spBasis(int maxE) {
    int c;
    int i, j, k, l;
    int q = 0;
    int** spStates;

    spStates = (int**)malloc(330 * sizeof(int *));
    for (c = 0; c < 330; c++) {
        spStates[c] = malloc(4 * sizeof(int));
    }

    for (i = 0; i <= maxE; i++) {
        for (j = i % 2; j <= i; j += 2) {
            for (k = -(2 * j  + 1); k <= (2 * j + 1); k += 2) {
                spStates[q][0] = i;
                spStates[q][1] = j;
                spStates[q][2] = 2 * j + 1;
                spStates[q][3] = k;
                q += 1;
            }
            for (l = -(2 * j - 1); l <= (2 * j - 1); l += 2) {
                spStates[q][0] = i;
                spStates[q][1] = j;
                spStates[q][2] = 2 * j - 1;
                spStates[q][3] = l;
                q += 1;
            }
        }
    }
    return spStates;
}

void mpBasis(int** spStates, int e, int maxE, int m, int M, int l, 
    int P, int * mpState, struct mpBasis *mpB, int position, int lastSpState) {
    int i;

    for (i = lastSpState; i < 330; i++) {
        if (e > maxE) {
            break;
        } else if (position == 5) {
            if (m == M && l % 2 == P) {
                 insertMpState(mpB, mpState);
                 break;
             }
        } else {
             // add spState to mpState and make the recursive call for the next position
            mpState[position] = i;
            mpBasis(spStates, e + spStates[i][0], maxE, m + spStates[i][3], M, 
                l + spStates[i][1], P, mpState, mpB, position+1, i);
        }
    }
}

void initMpBasis(struct mpBasis *a, int initialSize, int sizeY) {
    int c;
    a->basis = (int **)malloc(initialSize * sizeof(int*));
    for (c = 0; c < initialSize; c++) {
        a->basis[c] = (int *) malloc(sizeY * sizeof(int));
    }
    a->size = 0;
    a->capacity = initialSize;
}

void insertMpState(struct mpBasis *a, int* mpState) {
    /*if (a->size == a->capacity) {
        a->size *= 2;
        a->basis = (int **)realloc(a->basis, a->size * sizeof(int));
    }*/
    a->basis[a->size++] = mpState;
}

添加了所有代码。

问题是在递归函数被调用后,结构 mpBasis 中的“基础”数组仍然只包含随机值,即 mpBasis 函数没有对它做任何事情。我在这里按值传递 mp 参数吗?

谢谢你的帮助!

4

1 回答 1

0

第一步是在启用警告的情况下进行编译。例如,如果您使用 GCC,您可以使用选项-Wall -Wextra。

编辑

(先前列出的 >20 个错误已删除)

好的,既然您使用的是 Visual Studio,请启用如下警告:

  • 打开项目的属性页对话框。
  • 选择 C/C++。
  • 在常规属性页上,将警告级别修改为 /W4
于 2013-02-26T09:37:42.587 回答