Solved

Data Structure

Posted on 2004-10-25
193 Views
Last Modified: 2010-04-15
Dear All,
 I have the following data structure:

struct mydata
{
    unsign int numbit1:1;
    unsign int numbit2:1
    unsign int numbit3:1;
    unsign int numbit4:1;
    unsign int numbit5:1;
    unsign int numbit6:1;
    unsign int numbit7:1;
    unsign int numbit8:1;
};
but when I do

  mydata *pointStruct;
  unsign int value= 8;
  pointStruct = (mydata*)value;

pointStruct is null. Can you tell me what is wrong? or can you give any hint how to pass a value to a variable of such structure? I am trying to access each one the bits.
Thank you
0
Question by:hvelasco
    6 Comments
     
    LVL 14

    Assisted Solution

    by:RichieHindle
    You need to write to the data via a pointer to a char - here's a working version:

    #include "stdio.h"

    struct mydata
    {
        unsigned int numbit1:1;
        unsigned int numbit2:1;
        unsigned int numbit3:1;
        unsigned int numbit4:1;
        unsigned int numbit5:1;
        unsigned int numbit6:1;
        unsigned int numbit7:1;
        unsigned int numbit8:1;
    };

    int main(void)
    {
      struct mydata s;                               // Where the data is stored
      struct mydata* pointStruct = &s;               // Treat the data as a mydata
      unsigned char* pointInt = (unsigned char*) &s; // Treat the data as a char
      *pointInt = 8;                                 // Set the value
      printf("%d", pointStruct->numbit4);            // Extract one of the bits
      return 0;
    }

    0
     
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    I think must be:

    pointStruct = (mydata*)&value;
    0
     
    LVL 45

    Assisted Solution

    by:Kdo
    Hi hvelasco,

    A couple of things come into play here.  Your system may be big-endian or little-endian, meaning that the left-most or right-most byte is the most significant.  You may also need to know the size of the data elements, ans RichieHindle has pointed out.

    A couple of less obvious complications also exist.  Some compilers place restrictions on bit-fields.  These restrictions can allow the program to compile and only show up at run-time.  Data packing may become an issue as you try to test this.

    You can work past most of these errors with a little test.  Starting with your original code, run this:

    struct mydata
    {
        unsign int numbit1:1;
        unsign int numbit2:1
        unsign int numbit3:1;
        unsign int numbit4:1;
        unsign int numbit5:1;
        unsign int numbit6:1;
        unsign int numbit7:1;
        unsign int numbit8:1;
    };

    unsigned int value= 8;

    int  Endian = 1;
    char Buffer[8];

    int main ()
    {
      mydata *pointStruct;

      memcpy (Buffer, Endian, sizeof (int));
      pointStruct = (struct mydata *)value;

      if (Buffer[0] = 0)  /*  Big-endian  */
      {
        pointStruct += (sizeof (int) - 1);
        printf ("Big Endian\n");
      }
      else
        printf ("Little Endian\n");

      printf (" %d\n", pointStruct->numbit1);
      printf (" %d\n", pointStruct->numbit2);
      printf (" %d\n", pointStruct->numbit3);
      printf (" %d\n", pointStruct->numbit4);
      printf (" %d\n", pointStruct->numbit5);
      printf (" %d\n", pointStruct->numbit6);
      printf (" %d\n", pointStruct->numbit7);
      printf (" %d\n", pointStruct->numbit8);
    }



    Good Luck!
    Kent
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    jaime_olivares,

    Yep.  I dropped that in my example, too.

    sigh...

    0
     
    LVL 8

    Assisted Solution

    by:ssnkumar
    Hi hvelasco,

    Already you have got valuable comments for your post.
    Still I want to add my comment:-)
    I post the same code that you had posted with the corrections:

    struct mydata
    {
        unsigned int numbit1:1;
        unsigned int numbit2:1;
        unsigned int numbit3:1;
        unsigned int numbit4:1;
        unsigned int numbit5:1;
        unsigned int numbit6:1;
        unsigned int numbit7:1;
        unsigned int numbit8:1;
    };

      struct mydata *pointStruct;
      unsigned int value= 8;
      pointStruct = (struct mydata*)&value;

    Note these corrections:
    1. Use "unsigned" instead of "unsign".
    2. In C++, you are allowed to leave the word "struct", but not in C during variable declarations.
    3. Since pointStruct is a pointer, you need to give it a valid address before using it.
        In your code:
    >pointStruct = (struct mydata*)value;
        You were trying to assing the value (which is 8 in your code) to the address of pointStruct. That's why you were facing problem. Instead assing the address of value (which is on stack and will be a valid address) as I have shown. With this your code will work.

    Please do give feedback and let us know if you have any other problems......

    -ssnkumar
    0
     
    LVL 12

    Accepted Solution

    by:
    Hi hvelasco,
    Bitfields are one of C's darker corners. You can't use them in arrays, typedefs, etc, so basically they're not of much help. So don't use them unless you have to.

    typedef struct mydata_s
    {
        unsigned int numbit1:1;
        unsigned int numbit2:1;
        unsigned int numbit3:1;
        unsigned int numbit4:1;
        unsigned int numbit5:1;
        unsigned int numbit6:1;
        unsigned int numbit7:1;
        unsigned int numbit8:1;
    } mydata;

    unsigned int value= 8;

    int  Endian = 1;
    char Buffer[8];

    int main ()
    {
      mydata *pointStruct;

      memcpy (Buffer, &Endian, sizeof (int));
      pointStruct = (mydata *)&value;

      if (Buffer[0] = 0)  /*  Big-endian  */
      {
        pointStruct += (sizeof (int) - 1);
        printf ("Big Endian\n");
      }
      else
        printf ("Little Endian\n");

      printf (" %d\n", pointStruct->numbit1);
      printf (" %d\n", pointStruct->numbit2);
      printf (" %d\n", pointStruct->numbit3);
      printf (" %d\n", pointStruct->numbit4);
      printf (" %d\n", pointStruct->numbit5);
      printf (" %d\n", pointStruct->numbit6);
      printf (" %d\n", pointStruct->numbit7);
      printf (" %d\n", pointStruct->numbit8);
    }


    Cheers!

    Stefan
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now