2017-07-22 42 views
-1
二叉搜索樹錯誤
/* These are struct definitions I am using */ 
struct PdsNdxInfo{ 
    int key; 
    int offset; 
}; 

struct PdsInfo{ 
    FILE *repo_fptr; 
    FILE *ndx_fptr; 
    char repo_name[MAX_NAME_LEN]; 
    int repo_status; 
    int num_recs; 
    struct PdsNdxInfo ndxEntries[MAX_RECS]; 
}; 


/*This is the code */ 

//BST Creation 
struct PdsNdxInfo temp[pdsInfo.num_recs]; 
fseek(pdsInfo.ndx_fptr,0,SEEK_SET); 
fread(temp, sizeof(struct PdsNdxInfo), pdsInfo.num_recs, pdsInfo.ndx_fptr); 


int i=0; 
while(i < pdsInfo.num_recs){ 
    printf("********%d %d",temp[i].key,temp[i].offset); 
    if(root==NULL) { 
     root =insert(root,temp[i].key,temp[i].offset); //getting error 
    } 
    else { 
     insert(root,temp[i].key,temp[i].offset); 
    } 
    i++;  
} 

/* This is the function definition */ 
struct node *newNode(int k,int o){ 
    struct node *temp = (struct node *)malloc(sizeof(struct node)); 
    temp->key = k;temp->offset = o; 
    temp->left = temp->right = NULL; 
    return temp; 
} 

struct node* insert(struct node* root, int k,int o) { 
    if (root == NULL) return newNode(k,o); 
    if (k < root->key) 
    root->left = insert(root->left, k,o); 
    else if (k > root->key) 
    root->right = insert(root->right, k,o); 

    return root; 
} 

編譯器錯誤:c語言:在實現使用結構

pds_version2.c: In function ‘pds_store’: 
pds_version2.c:119:9: warning: assignment makes pointer from integer 
without a cast [enabled by default] 
root = insert(root,pdsInfo.ndxEntries[pdsInfo.num_recs- 
1].key,pdsInfo.ndxEntries[pdsInfo.num_recs-1].offset); 
    ^
pds_version2.c: At top level: 
pds_version2.c:180:14: error: conflicting types for ‘insert’ 
struct node* insert(struct node* root, int k,int o) 
     ^
pds_version2.c:66:10: note: previous implicit declaration of ‘insert’ 
was here 
root =insert(root,temp[i].key,temp[i].offset); 

無法弄清楚,爲什麼錯誤發生的事情基本上我想創建二叉搜索樹及以上是兩個方法插入和newnode但得到編譯時錯誤。

新節點是一個具有左右兩個指針和兩個數據值的結構。但我無法弄清楚爲什麼這樣的錯誤「賦值使指針從整數沒有投射[默認情況下啓用]」正在發生

+2

請縮進代碼,並加入到這個問題*「BST創造」的部分*離開。至少它是如何啓動的和「root」的聲明。 –

+0

請閱讀如何創建MCVE([MCVE])。當您創建一個MCVE時,您可以發佈編譯器錯誤以獲得您所顯示的確切代碼,以及完全匹配的行號。當然,現代GCC在錯誤信息中包含了它所抱怨的行的信息(它並不總是這樣,在那些日子裏,讓問題中的代碼與編譯器中的行號相匹配更重要消息),所以你可以更快地消除混雜。但是你仍然應該顯示可以提交給編譯器的代碼,以便獲得相同的錯誤。 –

回答

1

你應該把插入函數放在你使​​用它的地方之前。 或者乾脆把函數聲明之前:

struct node* insert(struct node* root, int k,int o); 

void code() 
{ 
    //use insert function here 
} 
struct node* insert(struct node* root, int k,int o) 
{ 
    //insert function definition 
} 
+0

謝謝@羅西88。再次感謝你(y)!! – amitabh