2017-05-27 88 views
0

我有一個結構定義爲:指針賦值錯誤C

typedef struct _InstNode{ 

    InstInfo* instinfo; 
    struct _InstNode *dep1; 
    struct _InstNode *dep2; 
    bool is_exit; 
    bool is_entry; 
    unsigned inst_latency; 
    unsigned depth_latency; 
} InstNode; 

,這是instnode_array:

InstNode *instnode_array; 
    instnode_array = (InstNode*)malloc(sizeof(InstNode)*numOfInsts); 

現在我正在努力做到以下幾點: -

instnode_array[i].dep1 = instnode_array[j]; 

我得到這個錯誤:

incompatible types when assigning to type 'struct _InstNode *' from type 'InstNode {aka struct _InstNode }'

instnode_array[i].dep1 = instnode_array[j]; 
+0

'instnode_array'不是數組.... –

+0

@SouravGhosh它是一個指針數組.. ..怎麼沒有? –

+3

指針數組?怎麼來的? –

回答

3

TL; DR注意尊重數據類型。

instnode_array[j]給你一個struct _InstNodedep1struct _InstNode *的類型。

你可能想要寫

instnode_array[i].dep1 = &(instnode_array[j]); 

如果你明白了。

+0

我明白了。它現在有效。 –

+0

另一個相關的問題如果我可能: 當我寫字段dep1這種方式:struct _InstNode * dep1;' 它給了我錯誤,但是,當我這樣寫:'struct _InstNode * dep1;'建立與沒有錯誤..任何解釋? –

+0

@Ameerkhan我沒有看到任何理由。檢查[這個答案](https://stackoverflow.com/a/44055041/2173917) –