2

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.

4 Answers 4

6

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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..
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..
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.

Comments

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

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.