【问题标题】:Trouble Dereferencing a double pointer to struct麻烦取消对结构的双指针
【发布时间】:2013-04-12 06:35:17
【问题描述】:

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

出现以下错误:

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

【问题讨论】:

  • 作业可能意味着double*
  • 那么,const PolicyNode*?我不认为有一个 typedef 。它是const?考虑到您将其成员之一分配给非常量ppTerm?并且在任何地方都看不到printFraction()。唔。如果我们从表面上看“工作正常”的评论,你会明白的。例如:printf(" X^%d", argTerm-&gt;expo) ? argTerm 是一个指向指针的指针。 (*argTerm)-&gt;expo 可能有更多的牙齿(并且几乎编译,除了成员不称为expo,称为exponent)。简而言之。有什么东西可以编译 ??

标签: c dereference


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2022-01-08
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2013-02-09
    • 2013-01-04
    相关资源
    最近更新 更多