简体   繁体   English

制作一个存储char数组元素的指针

[英]making a pointer that stores an element of a char array

I've been struggling trying to figure out why I am getting the following warning: 我一直在努力找出为什么我得到以下警告:
initialization makes pointer from integer without a cast 初始化使指针从整数开始而无需强制转换

The highlighted warnings are where I mentioned below. 突出显示的警告是我在下面提到的地方。 The code I am currently using is just the beginning of creating tree of elements in a linked list fashion. 我当前正在使用的代码仅仅是以链表形式创建元素树的开始。 This code seems to be working fine however I get docked points for warnings. 这段代码似乎工作正常,但是我得到了警告的停靠点。

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

Your fundamental mistake is that you are assigning a poitner to a char which is wrong 您的根本错误是您将Poitner分配给错误的char

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

this will not yield the result you expect. 这不会产生您期望的结果。

The * in this case is not dereferencing the pointe, you are declaring a poitner and pointeing to a char with it, then when you try to access the pointer, your program tries to read at an invalid memory address causing undefined behavior. 在这种情况下, *不是取消引用Pointe,而是声明一个Poitner并使用它指向一个char ,然后在尝试访问指针时,程序尝试在无效的内存地址进行读取,从而导致未定义的行为。

Then you do the opposite thing in 然后你做相反的事情

ptr->data = data;

you should enable compiler warnings to avoid this mistakes. 您应该启用编译器警告以避免此错误。

To handle the data you apparently want to handle, first you need to redefine the struct like this 要处理您显然要处理的数据,首先需要重新定义这样的结构

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

then in the TreeCreate() function, copy the data by first allocating space and then using memcpy() like this 然后在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;
}

I think I understand. 我想我明白。 The following fixed my warnings. 以下内容修复了我的警告。 Thanks for the fast response! 感谢您的快速回复!

const char *tree = &list[0];
ptr->data = *data;
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM