Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data Structure

Posted on 2004-10-25
6
Medium Priority
?
244 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
Comment
Question by:hvelasco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Assisted Solution

by:RichieHindle
RichieHindle earned 400 total points
ID: 12405004
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
Jaime Olivares earned 200 total points
ID: 12405058
I think must be:

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

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 total points
ID: 12405160
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12405170
jaime_olivares,

Yep.  I dropped that in my example, too.

sigh...

0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 400 total points
ID: 12407734
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:
stefan73 earned 600 total points
ID: 12419790
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

636 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