Friday 13 September 2013

Error when returning a pointer

Error when returning a pointer

I am practicing coding binary search tree in C and I ran into an error.
#include <stdio.h>
#include <stdlib.h>
/*struct Node*/
typedef struct Node{
int data;
struct Node* left;
struct Node* right;
}Node;
/*Forward declaration*/
Node *createNode(int data);
int main(int argc, char** argv) {
Node *root;
root = createNode(3); //ERROR
}
Node* createNode(int data){
Node* newNode = (Node*)malloc(sizeof(Node));
if(newNode==NULL){
fprintf(stderr,"Failed to allocate node\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right= NULL;
return newNode; //ERROR OCCURS HERE
}
I get a failed run when I try to run this. The error occurs during the
return newNode. I am not sure why the point is not returning.
I am using netbeans and this is what it says

No comments:

Post a Comment