• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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
0
hvelasco
Asked:
hvelasco
5 Solutions
 
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
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
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

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now