Thread: Error with strcmp

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    47

    Error with strcmp

    Hello,

    The compiler is reporting the following error
    Code:
    warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
    with this strcmp statement (line 106).
    Code:
    if (strcmp(summary_list->summary_record->c_detail1, summary_record->c_detail1 == 0))
    When the strcmp is executed a segmentation fault occurs, so I need to resolve the warning but I can't see what the issue is.

    The strcmp statement is in function: rem

    Here is the full code to demonstrate the issue
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct summary_record
    {
        char        c_detail1[15];
        char        c_detail2[100];
        float       f_value1;
        float       f_value2;
    };
    typedef struct summary_record summary_record_t;
    
    struct summary
    {
        struct summary_record  *summary_record;
        struct summary         *prev;
        struct summary         *next;
    };
    typedef struct summary summary_t;
    
    summary_t *add_at(int pos, summary_t *summary_list, summary_record_t *summary_record)
    {
        summary_t *node = malloc(sizeof(summary_t) * 1);
        if (node == NULL)
        {
            printf("Malloc failed in: add_at\n");
            getchar();
        }
        node->summary_record = malloc(sizeof(summary_record_t) * 1);
        if (node->summary_record == NULL)
        {
            printf("Malloc failed in: add_at: node->summary_record\n");
            getchar();
        }
    
        strcpy(node->summary_record->c_detail1, summary_record->c_detail1);
        strcpy(node->summary_record->c_detail2, summary_record->c_detail2);
        node->summary_record->f_value1 = summary_record->f_value1;
        node->summary_record->f_value2 = summary_record->f_value2;
        node->prev = NULL;
        node->next = NULL;
    
        // handle case where list is empty
        if (summary_list == NULL)
        {
            summary_list = node;
            return summary_list;
        }
    
        int idx = 0;
    
        summary_t *prev = NULL;
        summary_t *cur = summary_list;
    
        // walk through list until pos or end is reached
        while (cur != NULL && idx != pos)
        {
            ++idx;
            prev = cur;
            cur = cur->next;
        }
    
        // insertion point reached
    
        // beginning, includes list update
        if (idx == 0)
        {
            summary_list = node;
            node->next = cur;
            cur->prev = node;
            return summary_list;
        }
    
        // end
        if (cur == NULL)
        {
            prev->next = node;
            node->prev = prev;
            return summary_list;
        }
    
        // middle
        prev->next = node;
        node->prev = prev;
        node->next = cur;
        cur->prev = node;
        return summary_list;
    }
    
    summary_t *add_end(summary_t *summary_list, summary_record_t *summary_record)
    {
        summary_list = add_at(-1, summary_list, summary_record);
        return summary_list;
    }
    
    summary_t *rem(summary_t *summary_list, summary_record_t *summary_record)
    {
        // walk through list
        while (summary_list != NULL)
        {
            // Found record - detail1 matches
            if (strcmp(summary_list->summary_record->c_detail1, summary_record->c_detail1 == 0))
            {
                // handle first item
                if (summary_list->prev == NULL)
                {
                    if (summary_list->next == NULL)
                    {
                        // only item?
                        summary_list = NULL;
                    }
                    else
                    {
                        // more items?
                        summary_list = summary_list->next;
                        summary_list->prev = NULL;
                    }
                    free(summary_list->summary_record);
                    free(summary_list);
                    return summary_list;
                }
    
                // handle last item
                if (summary_list->next == NULL)
                {
                    summary_list->prev->next = NULL;
                    free(summary_list->summary_record);
                    free(summary_list);
                    return summary_list;
                }
    
                // handle middle item
                if (summary_list->prev != NULL && summary_list->next != NULL)
                {
                    summary_list->prev->next = summary_list->next;
                    summary_list->next->prev = summary_list->prev;
                    free(summary_list->summary_record);
                    free(summary_list);
                    return summary_list;
                }
            } // found record
    
            // Next record
            summary_list = summary_list->next;
    
        } // while (summary_list != NULL)
    } // rem
    
    void dump_fwd(summary_t *summary_list)
    {
        printf("Forward:\n");
    
        while (summary_list != NULL)
        {
            printf("Detail 1: %s\n", summary_list->summary_record->c_detail1);
            printf("Detail 2: %s\n", summary_list->summary_record->c_detail2);
            printf("Value 1:  %f\n", summary_list->summary_record->f_value1);
            printf("Value 2:  %f\n", summary_list->summary_record->f_value2);
            printf("\n");
            summary_list = summary_list->next;
        }
        getchar();
    }
    
    int main()
    {
        summary_t *summary_list = malloc(sizeof(summary_t) * 1);
        summary_list = NULL;
    
        summary_record_t *summary_record = malloc(sizeof(summary_record_t) * 1);
    
        strcpy(summary_record->c_detail1,"some detail");
        strcpy(summary_record->c_detail2,"more detail");
        summary_record->f_value1 = 100;
        summary_record->f_value2 = 200;
        summary_list = add_end(summary_list,summary_record);
    
        strcpy(summary_record->c_detail1,"next");
        strcpy(summary_record->c_detail2,"blah");
        summary_record->f_value1 = 50;
        summary_record->f_value2 = 100;
        summary_list = add_end(summary_list,summary_record);
        dump_fwd(summary_list);
    
        strcpy(summary_record->c_detail1,"next");
        strcpy(summary_record->c_detail2,"blah");
        summary_record->f_value1 = 50;
        summary_record->f_value2 = 100;
        summary_list = rem(summary_list,summary_record);
        dump_fwd(summary_list);
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably just need to rest your eyes and come back after a break. It is a matter of the closing parenthesis being in the wrong place. It should have been:
    Code:
    if (strcmp(summary_list->summary_record->c_detail1, summary_record->c_detail1) == 0)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp error
    By Akeold in forum C Programming
    Replies: 4
    Last Post: 03-11-2019, 05:34 AM
  2. strcmp error
    By p595285902 in forum C Programming
    Replies: 18
    Last Post: 03-23-2011, 03:53 PM
  3. Error with strcmp
    By nick048 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2006, 02:28 PM
  4. Strcmp error
    By Ron in forum C Programming
    Replies: 14
    Last Post: 06-17-2006, 03:06 PM
  5. strcmp error
    By rodrigorules in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2005, 12:54 PM

Tags for this Thread