2015-03-02 30 views
0

我一直在努力試圖爲什麼我收到以下警告要弄清楚的一個元素的指針:
初始化使指針從整數沒有投使得存儲字符數組

高亮警告我在下面提到。我目前使用的代碼只是以鏈表形式創建元素樹的開始。此代碼似乎工作正常,但我得到停靠點警告。

typedef struct Node { 
     struct Node *leftChild; 
     struct Node *rightChild; 
     char data; 
} Node; 

Node *TreeCreate(int level, const char *data) { 
    struct Node *ptr = (struct Node*) malloc(sizeof (Node)); 
    if (ptr == NULL) { 
     // malloc failed 
     return 0; 
    } 
    ptr->data = data; // WARNING 
    ptr->leftChild = NULL; 
    ptr->rightChild = NULL; 
    return ptr; 
} 
// TEST CODE IN MAIN 
char list[6] = {'A', 'B', 'C','\0'}; 

// Determines the element 
const char *tree = list[0]; // WARNING 
ptr = TreeCreate(1, tree); 
if (ptr != NULL) { 
    sprintf(string, "TreeData: %c\n", ptr->data); 
    OledDrawString(string); 
    OledUpdate(); 
} 
+0

首先[不投的malloc'的結果()'](http://stackoverflow.com/a/605858/1983495)。然後,請發佈'string'聲明。 – 2015-03-02 23:51:10

+0

typedef'ing結構總是一個壞主意,應該避免 – user3629249 2015-03-03 01:35:51

+0

在C中,malloc(和家族)返回的值不應該被轉換 – user3629249 2015-03-03 01:37:04

回答

1

你根本錯誤是你的char這是錯誤的

const char *tree = list[0]; // WARNING 

這不會產生你所期望的結果分配poitner。

在這種情況下,*不提領的足尖,你聲明poitner和pointeing與它char,那麼當您試圖訪問指針,你的程序會在無效的內存地址讀取導致未定義行爲。

然後你做相反的事情

ptr->data = data; 

你應該讓編譯器警告,以避免這種錯誤。

要處理,你顯然要處理的數據,首先你需要重新定義這樣

typedef struct Node { 
    struct Node *leftChild; 
    struct Node *rightChild; 
    char *data; 
    /* ^this should be a char pointer */ 
} Node; 

然後在TreeCreate()功能的結構,首先使用memcpy()這樣

分配空間,然後複製數據
Node *TreeCreate(int level, const char *data) { 
    size_t  length; 
    struct Node *ptr; 

    ptr = malloc(sizeof (Node)); 
    if (ptr == NULL) { 
     return NULL; 
    } 
    if (data != NULL) 
    { 
     length = strlen(data); 
     ptr->data = malloc(1 + length); 
     if (ptr->data != NULL) 
      memcpy(ptr->data, data, 1 + length); 
    } 
    else 
     ptr->data = NULL;   

    ptr->leftChild = NULL; 
    ptr->rightChild = NULL; 

    return ptr; 
} 
+0

此外,它是在代碼頂部的另一種方式。他分配一個指向char的指針(在'ptr-> data = data; // WARNING'中,因爲ptr-> data是一個char。 – russianfool 2015-03-02 23:53:27

+0

@russianfool是的,我認爲OP將'char' poitners與'char – 2015-03-02 23:55:26

0

我想我明白了。以下修正了我的警告。感謝您的快速響應!

const char *tree = &list[0]; 
ptr->data = *data; 
+0

如果這是你想要的,你不應該使用指針 – 2015-03-02 23:59:49

+0

是的,不知道爲什麼我們的教授讓我們使用Node * TreeCreate的指針輸入,但是也許它擁有更多的相關性,我只有剛開始搞這個任務。 – Connor 2015-03-03 00:06:41

0
the following, a complete program, 
that cleanly compiles 
and has the warnings fixed 
and eliminates the clutter and unnecessary typedef statements. 

#include<stdio.h> 
#include<stdlib.h> 


struct Node 
{ 
     struct Node *leftChild; 
     struct Node *rightChild; 
     char data; 
}; 

struct Node *TreeCreate(int level, const char *data) 
{ 
    struct Node *ptr = malloc(sizeof (struct Node)); 
    if (ptr == NULL) 
    { 
     // malloc failed 
     return NULL ; 
    } 

    // implied else, malloc successful 

    ptr->data = *data; // WARNING 
    ptr->leftChild = NULL; 
    ptr->rightChild = NULL; 
    return ptr; 
} 

int main() 
{ 
    struct Node *ptr = NULL; 
    char string[120] = {'\0'}; 

    // TEST CODE IN MAIN 
    char list[6] = {'A', 'B', 'C','\0'}; 

    // Determines the element 
    const char *tree = &list[0]; // WARNING 

    ptr = TreeCreate(1, tree); 

    if (ptr != NULL) 
    { 
     sprintf(string, "TreeData: %c\n", ptr->data); 
     //OledDrawString(string); 
     //OledUpdate(); 
    } 
    return 0; 
}