麻烦解除引用struct的双指针

时间:2013-04-12 06:35:17

标签: c dereference

我似乎无法读取双指针指向的数据。它是一个赋值,我必须使用双指针。

获得以下错误:

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;
}

1 个答案:

答案 0 :(得分: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;
}

另一种情况只是因为你很幸运。

相关问题