Thread: data structures

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    data structures

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct foo
    {
        unsigned int a:16;
        unsigned int b:8;
        unsigned int c:4;
    } *fooptr = NULL;
    
    int main( void )
    {
        if( (fooptr = malloc( sizeof *fooptr )) == NULL )
            printf( "malloc failed, that's bad\n" );
        else
        {
            fooptr->a = 1;
            fooptr->b = 2;
            fooptr->c = 3;
            free( fooptr );
        }
        return 0;
    }
    In this sorta of example i just wanted to know how to assign a whole number when you split up the int into a b c but if i know the whole unsigned int could i just assign it without
    fooptr->a = 1;
    fooptr->b = 2;
    fooptr->c = 3;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You wouldn't. Your structure doesn't allow for that. You are restricting the size of the varaible's allowed values here. Also, you aren't using a union, so it's not an "either or" sort of thing. It's a structure, that typically means it has multiple members.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    So theres not shortcut to defining the members all together its just a whole bunch of multiple definitions

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is this what you're looking for?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct foo 
    {
        unsigned int a:16;
        unsigned int b:8;
        unsigned int c:8;
    };
    
    struct bar 
    {
        union
        {   
            struct foo f;
            unsigned int i;
        };  
    };
    
    int main(void)
    {
        struct bar b1; 
    
        b1.i = 0x01234567;
    
        printf("a = %x\n", b1.f.a);
        printf("b = %x\n", b1.f.b);
        printf("c = %x\n", b1.f.c);
    
        return 0;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    I figured it out I wanted something like this to define the members in one go

    *(unsigned int *)&fooptr = 0x234324;

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That assumes of course that your structure is packed the way you expect it to be packed...


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should malloc for sizeof foo (the struct) instead of the pointer's size.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by nonoob View Post
    You should malloc for sizeof foo (the struct) instead of the pointer's size.
    He's not using the size of the pointer, he is using the size of the pointer's type. In other words, the way he has it is correct.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually he isn't using anything. That's my code example from another thread.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good generalized data structures book?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-16-2006, 01:01 PM
  2. i need advice about data structures
    By sawer in forum C Programming
    Replies: 2
    Last Post: 04-22-2006, 03:40 AM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. array of structures, data from file
    By nomi in forum C Programming
    Replies: 5
    Last Post: 01-11-2004, 01:42 PM
  5. Array Data Structures
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 06:52 PM