Thread: Struct compare

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    29

    Struct compare

    Hi everyone i have question how to compare some object of struct i have this code but it doesn't work.
    I compare color of fruits if some fruits have same color it needs to print them.
    Code:
    #include <stdio.h>struct Fruit
    {
        char name[20];
        char color[15];
        int weight;
    };
    void Print(struct Fruit S)
    {
        printf("%s - %s - %dg",S.name,S.color,S.weight);
    }
    void Compatible(struct Fruit S[],int n)
    {
        int i;
        for(i=0; i<n; i++)
        {
        if(S[i].color==S[i+1].color)
        {
            Print(S[i]);
        }
    } 
    }
    int main()
    {
        struct Fruit S[5];
        int i,n;
        printf("Enter number of fruits");
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            printf("Enter name of %d fruit:",i+1);
            scanf("%s",S[i].ime);
            printf("Enter color of %d fruit:",i+1);
            scanf("%s",S[i].boja);
            printf("Enter weight of %d fruit:",i+1);
            scanf("%d",&S[i].masa);
        }
        printf("===========================Fruits====================\n");
        for(i=0;i<n; i++)
        {
        Printi(S[i]);    
        }
        printf("===================Compatible fruits=============\n");
        {
            Compatible(S,n);
        }
    }
    P.S. Sorry for bad English

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Be sure you are compiling with maximum warnings, and post your error messages.

    Code:
    main.c||In function 'main':|
    main.c|31|error: 'struct Fruit' has no member named 'ime'|
    main.c|33|error: 'struct Fruit' has no member named 'boja'|
    main.c|35|error: 'struct Fruit' has no member named 'masa'|
    main.c|40|warning: implicit declaration of function 'Printi'|
    ||=== Build finished: 3 errors, 1 warnings ===|
    You're trying to store values into struct members that don't exist. Those should match the variable names in the struct ("name", "weight", and "color").

    Code:
    if(S[i].color==S[i+1].color)
    You can't compare strings with == in C, you need to use "strcmp()".

    Code:
    Printi(S[i]);
    This appears to be typo. Note that the last warning posted above is warning you that there is something wrong with this line.


    Edit: Your code could potentially attempt to access the array out of bounds.

    Code:
    struct Fruit S[5];
    int i,n;
    printf("Enter number of fruits");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    If the user enters a value greater than 5, you will go outside the bounds of your array.

    Code:
    if(S[i].color==S[i+1].color)
    If the user enters the maximum value, the [i+1] will go outside the bounds of your array.
    Last edited by Matticus; 03-04-2016 at 09:00 AM.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Ohh i forgot that strings I need to compare with strcmp function thanks for help, and that warnings and errors are from that i forgot to translate that parts to English.
    Anyway thanks for help friend

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You're welcome. Be sure to read the edit I just added to my previous message.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Now i see problem with get out of array, can you help me to fix this.
    Code:
    int Compatible(struct Fruit S[],int n){
        int i;
        for(i=0; i<n; i++)
        {
        if(strcmp(S[i].color,S[i+1].color)==0)
        {
            Print(S[i]);
            Print(S[i+1]);
        }
    } 
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Can you ask your question more clearly? I'm not sure I understand what you're asking here ("problem with get out of array").

    Also note you still have a potential array overrun.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Yes i talk about array overrun how to fix that, it's work normaly for 1-2-3-4 elements but when i type 5 elements my program close.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Arrays run from zero to "n-1".

    So if you have an array of size 5, then the valid indices are 0 - 4.

    So if we assume an "n" of 5, then this:

    Code:
    for(i=0; i<n; i++)
    ... means that "s[i]" runs through s[0] to s[4], which is fine. But ..

    "s[i+1]" runs through s[1] to s[5], which is out of bounds.

    If you don't want "s[i+1]" to run over the bounds of your array, then you just need to cycle one less time below the maximum.

    Code:
    for(i=0; i<n-1; i++)
    Last edited by Matticus; 03-06-2016 at 07:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. How to compare, without using compare (homework)
    By Mr.777 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2011, 02:55 AM
  3. compare with chars && compare with int
    By zcrself in forum C Programming
    Replies: 1
    Last Post: 04-22-2010, 03:19 AM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Compare Struct Variables
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 12:06 PM