lundi 2 mars 2015

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 begining of creating tree of elements in a linked list fashion. This code seems to be working fine however I get docked points for warnings. Any input or clarification to why this is happening would be great. Thanks guys.



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



Aucun commentaire:

Enregistrer un commentaire