将多行宏用于if语句时编译时错误

时间:2016-12-15 05:00:33

标签: c linux gcc macros

#include "header.h"
 #define CHECK_NULL(newnode) if(newnode == NULL)\
                 { \
                    printf("\n Memory not allocated...Sorry \n");\
                exit(FAILURE);\
                }
        /* insert first */

        node* insert_first(
            node* head /* parameter for head of the list*/
            ,int data/* parameter for data to be inserted */
            )
        {
            /* 1. declare a new node */
            node* newnode;

            /* 2. allocate memory to new node */
            newnode=(node*) malloc(sizeof(node));

            /* 3. check */
                /* 3.1 if memory is allocated or not*/
                CHECK_NULL(newnode)
                else{

                /* 3.2 else assign data */
                newnode->data=data;

                /* 3.3 pointer of this new node will point to head */
                newnode->next=head;

                /* 3.4 head will point to newnode */

     head=newnode;

            /* 3.5 return head so that you can print it in main */
            return head;
            }
    }

    /* insert_after */
    node* insert_after(
        node*head /*parameter for head of the list*/
        ,int node_number /* Parameter for knowing the place after which you want to insert the node */
        ,int data /* parameter for the data to be inserted at the end */)
    {
        /* 1. declare a new node-same as abv */
         node* newnode;

        /* 2. declare a current pointer to know the current location you want pointing to */
        node* curr;

        /* 3. declare count that counts the place where you are right now */
        int count=0;

        /* 4. allocate memory to the new node */
        newnode=(node*)malloc(sizeof(node));

        /* 5. check if the memory is allocated */
        CHECK_NULL(newnode)

     /* 6. assign data to the new node*/
        newnode->data=data;

        /* 7. assign link to null for the new node */
        newnode->next=NULL;

        /* 8. if list is empty or number at which you want to insert node is 0 i.e beginning */

                if((NULL == head) || (0 == node_number))
                {
                /*1. let head point to new node */
                head=newnode;

                /*2. return your head */
                return head;

                }

        /* 9 current pointer points to the same place as head */
            curr=head;

        /* 10. traverse the linked list till the current->next is not equal to Null */
                while(curr->next == NULL)
                {
                /* 1. increment the counter */
                count++;

      /* 2. if counter is equal to the place you want to insert node */
                    if(count == node_number)
                    {
                    /*1. next of new node will point to the node next to the current node */
                    newnode->next=curr->next;

                    /*2. next of current node will point to the newnode */
                    curr->next=newnode;

                    /*3. return your head after inserting the node */
                    return node;

                    }
                    curr=curr->next;
                }

                /* 11. Insert in end if no. of nodes are not sufficient*/
                curr->next=newnode;

    }

我无法在代码中使用多行宏。 我尝试使用通过互联网给出的do()while(0)解决方案。 但它对我没有用 你能帮我解决这个问题吗? :)

这是一个链接列表程序,可以在开头插入或插入某个地点之后。

这是错误: enter image description here

1 个答案:

答案 0 :(得分:0)

我想FAILURE的定义类似于#define FAILURE -1;;是多余的。                     –́BLUEPIXY