Thread: structures

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    structures

    Hi

    I was wondering if there is a way to loop over structure's components.

    For example:
    Code:
    typedef struct myStructure myStructure;
    struct myStructure {
    		char *name;
    		double len;
                                    int n;
                                    [etc.]
    		};
    myStructure a,b;
    in my case a is the "parent" of b.
    so after assigning everything for a and copying it to b, I want to check if some of the input I have for b is different from it's parent in which case I will update b with the new input (the input for b can be incomplete (so it might have a name but not len for example) and that is why I need to copy the components of a (which is always complete) first and update only the fields that were given as input for b).

    Sorry if my explenation of what I want to do is too complicated... not to sure how to write it...

    in pseudo code it should be like this:

    for component in b:
    component=a.component
    for eachComp in input:
    b.component=eachComp

    THANKS!!!!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The only way that u can check between the structs is to compare with each elements. Thats the only wya u can do it. But if u want to copy the content of one struct to another which are of same struct then u could use the '=' operator.

    Code:
    struct myStructure  a, b;
    
    a = b;
    ssharish

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is a way, but it isn't safe and it isn't portable. This assumes that you know the size of each member. It can also be a problem if the compiler pads your struct.
    The safest way is simply to examine all the members and see if they're equal.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to compare if two structs contain exactly the same thing, you can use memcmp() - however, it relies on the fact that you ALWAYS fill the struct with zero before you set any of the other data. This is due to what Elysia hints at: Structs usually have alignment padding which makes "holes" - these holes are "undefined" unless you fill the structure with something well defined (and zero is the best value here, as it also means that any numeric values are zero).

    Beware however that if you have pointers, the value of the pointer itself has to be equal. For example the "name" pointer in your struct, if you do:
    Code:
    mystructure a, b;
    
    ...
    char *str = "Abcde";
    a->name = malloc(strlen(str)+1);
    strcpy(a->name, str);
    ...
    b->name = malloc(strlen(str)+1);
    strcpy(b->name, str);
    ...
    if (memcmp(a, b, sizeof(a)) == 0) {
        printf("They are equal\n"); 
    }
    ...
    This code will NOT say "They are equal", since the two pointers to "Abcde" have different values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    Thanks guys!!
    I will go with the element by element options I guess... )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM