• C

Data Structure

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
hvelascoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RichieHindleCommented:
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
Jaime OlivaresSoftware ArchitectCommented:
I think must be:

pointStruct = (mydata*)&value;
0
Kent OlsenData Warehouse Architect / DBACommented:
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
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

Kent OlsenData Warehouse Architect / DBACommented:
jaime_olivares,

Yep.  I dropped that in my example, too.

sigh...

0
ssnkumarCommented:
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
stefan73Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.