Thread: strcmp error

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    strcmp error

    I have this structure:

    Code:
    typedef struct{
        string grapeSet[5][100];
    }tGrapeSet;
    I'm trying to compare if a grape exisists, but got 'incompatible type of argument 1":

    Code:
    tGrapeSet *find_grape(tCellar c, string grape)
    /*************************************************************************/
    {
        tGrapeSet *g;
        int ii;
        
        ii=0;
        g = NULL;
        while((ii < c.numWines) && (g==NULL)){
            if(strcmp(c.wines[ii].wines->grapeSet, grape))
                g = &(c.wines[ii].wines->grapeSet);
            else
                ii++;
        }
        return g;
       
    }
    do you know why?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have you got a typedef for "string"?

    What is it?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, what is string? My guess is that it is a typedef for char*, but don't do that: use char* directly, or else create a string struct that encapsulates a char*. Avoid pointer typedefs except for opaque pointer types.

    Now, if string really is a typedef for char*, then grapeSet is an array of 5 arrays of 100 pointers to char. This means that c.wines[ii].wines->grapeSet is an array of 5 arrays of 100 pointers to char, which is then converted to a pointer to the first element of an array of 5 arrays of 100 pointers to char, but strcmp requires its arguments to be pointers to char.
    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

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Sorry, you're right. Changed to char. But, then how can "initialize" this array?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Akeold
    Changed to char. But, then how can "initialize" this array?
    So, you're saying that you now have this?
    Code:
    typedef struct{
        char grapeSet[5][100];
    } tGrapeSet;
    
    /* ... */
    
    tGrapeSet *find_grape(tCellar c, const char *grape)
    {
    Notice that I used a const char* for grape because if you're trying to find a grape in a cellar, you aren't going to change the grape, so const should be used to avoid accidentally changing the string.

    Populating this array is a matter of making sure that you set the initial values of the grape names to be empty strings, and then later when actually populating it, checking that the grape set entry is an empty string before writing to it, e.g.,
    Code:
    void init_grapeSet(tGrapeSet *grapeSet)
    {
        size_t i;
        for (i = 0; i < sizeof(grapeSet) / sizeof(grapeSet[i]); ++i)
        {
            grapeSet[i][0] = '\0';
        }
    }
    
    int append_grape(tGrapeSet *grapeSet, const char *grape)
    {
        size_t i;
    
        /* This assert is useful if you know that the grape name is supposed to fit
           into the grape set. If that is not guaranteed, then this must not be an
           assert, but rather you need more complex error handling: */
        assert(strlen(grape) < sizeof(grapeSet[0]));
    
        for (i = 0; i < sizeof(grapeSet) / sizeof(grapeSet[i]); ++i)
        {
            if (grapeSet[i][0] == '\0')
            {
                strcpy(grapeSet[i], grape);
                return 1;
            }
        }
        /* the grape set is full: */
        return 0;
    }
    It would be more time efficient if you recorded the number of entries in use in each grape set, though of course it would be less space efficient since your grape set would then have an additional member.

    Your find_grape function likewise needs a loop to loop over the grapes in each grape set to find a match. I would suggest separating it into two functions:
    Code:
    tGrapeSet *find_grape_in_cellar(const tCellar *cellar, const char *grape);
    int check_grape_in_grapeSet(const tGrapeSet *grapeSet, const char *grape);
    Last edited by laserlight; 03-11-2019 at 05:37 AM.
    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 p595285902 in forum C Programming
    Replies: 18
    Last Post: 03-23-2011, 03:53 PM
  2. Error with strcmp
    By nick048 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2006, 02:28 PM
  3. Strcmp error
    By Ron in forum C Programming
    Replies: 14
    Last Post: 06-17-2006, 03:06 PM
  4. strcmp error
    By rodrigorules in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2005, 12:54 PM
  5. Can't figure out strcmp error
    By Fyodorox in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2002, 10:18 AM

Tags for this Thread