1

I return a error code from create_node function so that if memory not available it returns to the main and the program ends. I get an error that comparison of integer to pointer. Can you please help me to deal with it. I am a beginner. Thanks.


node *create_node(void)
{
    node *newnode = (node *)malloc(sizeof(node));

    if (newnode == NULL)
        return 1;
    newnode->right = NULL;
    newnode->left = NULL;
    return newnode;
}
int main(void)
{
    int ret_val = 0;
    node *root = create_node();

    if (root == 1) {
        printf("Memory not available to create a node\n");
        return 0;
    }
    root->left = create_node();
    if (root->left == 1) {
        printf("Memory not available to create a node\n");
        return 0;
    }
    root->right = create_node();
    if (root->right == 1) {
        printf("Memory not available to create a node\n");
        return 0;
    }
}

2 Answers2

0
 node *root = create_node();
 if(root == 1)

root is a pointer to node which you compare to the int value of 1. That´s why the error of comparing an integer with a pointer occurs.

It is not allowed to compare a pointer with an int in C. At least not if you didn´t casted the int to a pointer type.


Solution:

You should return a NULL pointer if the allocation in create_node() fails instead of returning 1:

 node *newnode = malloc(sizeof(*newnode));

 if (newnode == NULL)
 {
     return NULL;
 }

And then check root for NULL in main():

 node *root = create_node();

 if (root == NULL)
 {
     fprintf(stderr,"Memory not available to create a node\n");
     return 1;
 }

Side notes:

  1. When an error occurs, the return value of main() shall be non-zero (1 is common).
  2. You do not need to cast the result of malloc(). Here is a link: Do I cast the result of malloc
  3. malloc(sizeof(*newnode)) - Using this ensures that the allocated space will always fit to the object the pointer newnode points to (Important if you later want to change the structure pointed to by newnode).
Community
  • 1
  • 1
0

According to the C Standard (6.5.9 Equality operators) relative to comparisons of pointers and integers there is written

2 One of the following shall hold:

— both operands have arithmetic type;

— both operands are pointers to qualified or unqualified versions of compatible types;

— one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; or

— one operand is a pointer and the other is a null pointer constant.

So you may not compare a pointer and an integer without casting.

But in any case there is no need to return an integer in the case when a memory allocation will fail. In such a case it is enough to return NULL.

So the function can look like

node * create_node(void)
{
    node *newnode = malloc( sizeof( node ) );

    if  ( newnode != NULL )
    {
        newnode->right = NULL;
        newnode->left  = NULL;
    }

    return newnode;
}

Thus in main you can write for example

node *root = create_node();

if ( root == NULL ) {
    printf("Memory not available to create a node\n");
    return 0;
}

without getting the error message of the compiler.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335