在递归函数中使用结构(引用结构)

时间:2013-02-26 08:26:27

标签: c recursion structure

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

结构在函数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参数吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

修改

(删除了> 20个错误的上一个列表)

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

  • 打开项目的“属性页”对话框。
  • 选择C / C ++。
  • 在常规属性页面上,将警告级别修改为/ W4