1

Here is my code..

#include <stdio.h>

struct new {
    unsigned short b0 = 0;
    unsigned short b1 = 0;
    unsigned short b2 = 0;
    unsigned short b3 = 0;
    unsigned short b4 = 0;
    unsigned short b5 = 0;
    unsigned short b6 = 0;
    unsigned short b7 = 0;
};

int main()
{
    printf("Bit Field Example\n");
    struct new b; //Seems wrong to me
    int result = sizeof(b)/sizeof(*b); //Seems wrong to me
    printf("Size: %d\n", result);
    return 0;
}

I am using a linux machine to compile the mentioned code. I know that the line below is wrong..

int result = sizeof(b)/sizeof(*b);

But I am not sure about any other technique. First of all, Is it possible to count total number of elements in the structure.? Please give me some idea about how to do it.

Thanks in advance.

Alex
  • 9,891
  • 11
  • 53
  • 87
Hiren Pandya
  • 989
  • 1
  • 7
  • 20

4 Answers4

5

Not portably, generally, or safely, no.

Since the compiler might add padding for alignment and other reasons, which will make the size grow, it's not possible to rely on the value being correct.

If the number of elements matters, it's best to use an array.

By the way, this code:

int result = sizeof(b)/sizeof(*b);

is wrong and won't compile; b is not a pointer so *b is not a legal expression. You meant:

const int result = sizeof b / sizeof b.b0;

Also, as a side note, avoid using C++ keywords in C code, it can be somewhat confusing.

UPDATE I don't understand your reference to "bit fields". They look like this, and can only occur in structs:

struct strawberry {
  unsigned short fields : 10;
  unsigned short forever : 6;
};

Again, it's not possible to count them, since they don't even have byte sizes. But on the other hand, you wrote the definitions including the bit-widths for each field, so you should know how many there are, right?

unwind
  • 391,730
  • 64
  • 469
  • 606
  • So according to you, Its not possible to count number of elements in the structure.. right ? Then, is it possible to define bit fields using an array ? I mean, would that be correct syntax or something else.. – Hiren Pandya Apr 04 '13 at 11:31
  • hmmm... Thnx for the clear example.. I found the mistake of mine... Thnx a lot.. But here, one more question arises that whether or not these bit fields can be accessed similarly to the elements of the structure.. – Hiren Pandya Apr 04 '13 at 11:38
3

Using the sizeof(x) / sizeof(*x) is used for arrays not for structures. Since a structure is not of variable size, all fields are fixed at compilation, there is really no need to calculate the number of elements.

If you want to know the number of fields there are in a structure, you have to count them yourself.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

It is not possible to do this at runtime due to C being statically typed. Therefore the only way would be to use a macro. The macro can then generate this number at compile time. I have shown how to do this in another post.

The basic idea is to use X_MACRO's which allow you to define special X functions that can do something with your struct members

Community
  • 1
  • 1
smac89
  • 39,374
  • 15
  • 132
  • 179
-1

If you want the count of the number of elements in a structure is:

struct tag=&p;

If you increment the p++ and every time increment the count and print the count:

p++;

if(i=o;i<arr[i];i++)

count++;
printf("%d",count);

I think it is correct.

jvperrin
  • 3,368
  • 1
  • 23
  • 33