-->

Malloc'ing and resetting the array every time

2019-03-05 17:20发布

问题:

struct variables {

    unsigned int counter;
    char *bra;
    unsigned int maxb;
    int *findtheking;
    unsigned int numoright;
};

int getlen = 0; // I give getlen a value in another function
int solo = 0;
mat.bra = (char*)malloc(sizeof(char)*getlen);
mat.bra = '\0';

struct variables pal = { 0, '\0', 0, 0, 0 };
struct variables mat = { 0, '\0', 0 , 0, 0 };
struct variables all = { 0, '\0', 0,  0, 0 };
struct variables alle = { 0, '\0', 0 , 0, 0};

Visual Studio is telling me that

mat.bra = (char*)malloc(sizeof(char)*getlen);
*mat.bra = '\0';

both needs ';' and I don't see why. Please help?

and will it be possible for me to malloc for mat.bra if I

int matches(char* str, int len) {

getlen = strlen(str);


if (mat.counter >= strlen(str)) {
    if (mat.bra[strlen(str)-1] == '(') {
        mat.counter = 0;
        mat.bra[0] = '\0';
        return 0;
    }
    else if (mat.bra[strlen(str)-1]  == '0') {
        mat.counter = 0;
        mat.bra[0] = '\0';
        return 1;
    }


}
else {
    if (str[mat.counter] == '(') {
        mat.bra[len-1] = str[mat.counter];
        mat.counter++;
        matches(str, len - 1);

    }
    else if (str[mat.counter] == ')') {
        if (mat.bra[len] == '(') {
            mat.counter++;
            mat.bra[len] = '0';
            matches(str, len + 1);
        }
        else {
            mat.counter = 0;
            mat.bra[0] = '\0';
            return 0;
        }
    }
    else {
        mat.counter++;
        matches(str, len);
    }
}

}

have this function under the first chunk of code?

I'll run matches in main.c, and those codes are in func.c

I'm trying to run this function matches in main.c several times, and I need mat.bra to have different size strlen(str) every time it runs. Is it even possible for me to malloc in this way?

Sorry if my question is vague, I'm new to coding. If anything needs improvement, tell me and I'll try to elaborate more!