C structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyCGal
    New Member
    • Oct 2006
    • 6

    C structures

    Hi,

    I have three questions about C structures.

    First,
    How do I calculate the total number of members in the structure?
    for example,
    struct A{
    int a;
    int b;
    }
    Is it equal to sizeof(A)/sizeof(int)? But this cannot be generalized if a struct has members of different types.
    How do I do this?

    Second,
    Is it possible to define new members to struct later after the definition, like maybe in main()?

    Third,
    I have tried to reintialize a struct variable using
    struct A test={0,2};

    Later, it is changed to test={3,5};
    This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

    Thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    First,
    How do I calculate the total number of members in the structure?

    You can't, not only can you not do this but I can't think of any reason why you'd want to.

    Second,
    Is it possible to define new members to struct later after the definition, like maybe in main()?

    No, but if you where to put pointers in you structures you could then allocate a variable size array depending on what was required by the program.

    Third,
    I have tried to reintialize a struct variable using
    struct A test={0,2};

    Later, it is changed to test={3,5};
    This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

    You can only initialise a structure to an agregate ( {3,5} ) you can not use an agregate in an assignment.

    Comment

    • MyCGal
      New Member
      • Oct 2006
      • 6

      #3
      Originally posted by Banfa
      First,
      How do I calculate the total number of members in the structure?

      You can't, not only can you not do this but I can't think of any reason why you'd want to.

      Second,
      Is it possible to define new members to struct later after the definition, like maybe in main()?

      No, but if you where to put pointers in you structures you could then allocate a variable size array depending on what was required by the program.

      Third,
      I have tried to reintialize a struct variable using
      struct A test={0,2};

      Later, it is changed to test={3,5};
      This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

      You can only initialise a structure to an agregate ( {3,5} ) you can not use an agregate in an assignment.
      Hi,

      Thanks for all your answers.

      I have more Qs. Suppose I have a struct with 8 1-bit fields. I would like to display only certain number of bits. I have a display(struct) that displays all the 8 1-bit fields. Is it possible to display only certain number of bits of this struct using display(struct a, int number_of_bits_ to_show)?
      I was thinking of converting the bits into a equivalent deceimal value and then using masks and shifts to display the reqd number of bits.

      I would appreciate your comments on this.

      Thanks!

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Yeah, but say I want to show 5 bits. Which 5 bits do I show? 0-4, 1-5, 2-6, or 3-7?

        Code:
        void show_bits(const char bits, const int start, const int num_bits)
        {
          for(int i = start; i < (start+num_bits); i++)
            cout << ((bits >> i) && 1);
        // equivalently cout << ((bits && (1<<i))>>i);
         
          cout << endl;
        }

        Comment

        • MyCGal
          New Member
          • Oct 2006
          • 6

          #5
          Hi,

          Thanks for the code snippet. I assume that you have stored the 8 1-bit fields of the structure into char array "bits". And yes, I want to display any number of bits starting from anywhere in the bit string.

          Comment

          Working...