Thread: Structures to Equal

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    29

    Structures to Equal

    Code:
    #include <stdio.h>
    
    	struct bb {
    		unsigned char a : 3;
    		unsigned char b : 2;
    		unsigned char c : 2;
    		unsigned char d : 1;
    	} b;
    	
    	struct aa {
    		unsigned char a : 3;
    		unsigned char b : 2;
    		unsigned char c : 2;
    		unsigned char d : 1;
    	} a;
    
    int main (int argc, char * const argv[]) {
    	b.a = 3;
    	b.b = 2;
    	b.c = 1;
    	b.d = 0;
    
    	//a = b;
    	
    	printf("%d\n", b);
    	
    	printf("a : %d\nb : %d\nc : %d\nd : %d\nsize of bb : %d\n", b.a, b.b, b.c, b.d, (int)sizeof(b));
    
    	printf("a : %d\nb : %d\nc : %d\nd : %d\nsize of aa : %d\n", a.a, a.b, a.c, a.d, (int)sizeof(a));
    
    getchar(); <-- simple way to keep open
    }
    I'm currently using a bitfield to set data. (This is just for practice, not actual use).
    I'm wondering if there is any possible way to take one structure, have another one equal it. Or should I just quit using bitfield, and manipulate a 1 bit char?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could always use memcpy():
    Code:
    memcpy(&a, &b, sizeof(b));
    If you understand what you're doing, you're not learning anything.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    .....
    2) If i understand correctly, you want to have only a bit of data. That's impossible because bits don't have addresses, bytes do. A byte is the smallest addressable block of information in a computer.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Generally speaking, you shouldn't wholesale compare two structs of differing types. In this particular case, they're laid out the same, so it should work. What you'd have to do is get the address of each one and run memcmp() against them.

    EDIT: I misread. For an assignment, memcpy() would work, but again not generally a good idea to do this with differing types.
    Last edited by Clairvoyant1332; 02-22-2011 at 01:40 PM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    29
    Quote Originally Posted by itsme86 View Post
    You could always use memcpy():
    Code:
    memcpy(&a, &b, sizeof(b));
    Never needed to use memcpy before.
    Okay, thank you. I'll try that.

    ----

    Splendid.

    Output:
    51
    a : 3
    b : 2
    c : 1
    d : 0
    size of bb : 1
    a : 3
    b : 2
    c : 1
    d : 0
    size of aa : 1
    Last edited by Crosility; 02-22-2011 at 01:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM