2013-04-12 45 views
-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; 
} 
+0

這個賦值可能意味着'double *'? –

+0

那麼'const PolicyNode *'?我不認爲有這樣的typedef。它是'const'?考慮到你將它的一個成員分配給一個非const的ppTerm?沒有'printFraction()'在任何地方都可以看到。嗯。你會明白我們是否能以面值的方式做出「正常工作」的評論。例如:'printf(「X ^%d」,argTerm-> expo)'? 'argTerm'是一個指針指針。 '(* argTerm) - > expo'可能有更多的齒(和*幾乎*編譯,除非該成員不叫'expo',被稱爲'exponent')。簡而言之。有什麼*編譯*? – WhozCraig

回答

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)->exponent); 
    return; 
} 

因爲你是如此幸運,另一種情況只能更換

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

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