-1

我似乎无法读取双指针指向的数据。它用于分配,因为我必须使用双指针。

出现以下错误:

Error: Access violation reading location. 

这是代码:

struct Fraction {

        int num, denom;<br>
};
struct PolyTerm {

        struct Fraction coeff;
        int exponent;v
};
struct PolyNode {

    struct PolyTerm** dataPtr;
    struct PolyNode* next;
};

void printPolyTerm(struct PolyTerm** argTerm) { // this function works fine><br>

        printFraction(&(argTerm->coeff));       //also works fine
        printf(" X^%d", argTerm->expo);
        return;
}
void printPolyNode(const PolyNode* node) {  //NOT WORKING<br>

        struct PolyTermPS** ppTerm = node->dataPtr;
        struct PolyTermPS* pTerm = *ppTerm;
        printPolyTerm(pTerm);
        return;
}
4

1 回答 1

0

函数void printPolyTerm(struct PolyTerm** argTerm)接受双指针,因此,您的调用void printPolyNode(const PolyNode* node)必须更改:

void printPolyNode(const PolyNode* node) {  //NOT WORKING<br>

    struct PolyTermPS** ppTerm = node->dataPtr;
    struct PolyTermPS* pTerm = *ppTerm;
    printPolyTerm(pTerm);
    return;
}

一定是

void printPolyNode(const PolyNode* node) {  //NOT WORKING<br>

    struct PolyTerm** ppTerm = node->dataPtr;
    struct PolyTerm* pTerm = *ppTerm;
    printPolyTerm(&pTerm);
    return;
}

现在,在void printPolyTerm(struct PolyTerm** argTerm)函数内部,您必须取消引用双指针,我的意思是:

  • argTerm是指向指针的struct PolyTerm指针。
  • *argTerm是一个指向struct PolyTerm

所以,你必须更换

void printPolyTerm(struct PolyTerm** argTerm) { // this function works fine><br>

    printFraction(&(argTerm->coeff));       //also works fine
    printf(" X^%d", argTerm->expo);
    return;
}

经过

void printPolyTerm(struct PolyTerm** argTerm) { // this function works fine><br>

    printFraction(&((*argTerm)->coeff));       //also works fine
    printf(" X^%d", (*argTerm)->exponent);
    return;
}

另一种情况之所以有效,只是因为你太幸运了。

于 2013-04-12T06:59:18.203 回答