+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Member level 5
    Points: 1,626, Level: 9

    Join Date
    Nov 2010
    Posts
    82
    Helped
    0 / 0
    Points
    1,626
    Level
    9

    Structure value assign to an array

    Hi guys
    I want to assign structure value in an array. Is it possible?
    example what i want to do is below

    Code:
    struct paramerters{
     unsigned char position1:4;
     unsigned char position2:4;
     unsigned char position3:4;
     unsigned char position4:4;
    }A;
    
    unsigned char tab[] = {0x00,0x00};
    
    void main()
    {
      while(1)
      {
        A.position1 = 1;
        A.position2 = 2;
        A.position3 = 4;
        A.position4 = 8;
    
        tab[0] = A.position1 | A.position2 | A.position3 | A.position4;
      }
    }
    Here A is the structure, after assigning values to it, i copy these values in array tab[0].
    I want to ask can i do something like that
    tab[0] = A
    to assign the structure A values in array instead of
    tab[0] = A.position1 | A.position2 | A.position3 | A.position4;
    ?
    Hope i explained it properly.

    •   Alt9th November 2017, 09:33

      advertising

        
       

  2. #2
    Super Moderator
    Points: 24,476, Level: 38
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Portugal
    Posts
    7,350
    Helped
    917 / 917
    Points
    24,476
    Level
    38
    Blog Entries
    5

    Re: Structure value assign to an array

    For this case in particular, you could create a 32bits length variable to store the whole content of the 4 nibbles inside. Some compilers provide directives which instructs compiler to assemble inline the parameters within a structure, ensuring that would not use for example 8 bit length variables to store 4 bits. In cases on which data are stored nested without gaps, you can thing about using the memcpy function.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   Alt10th November 2017, 23:52

      advertising

        
       

  3. #3
    Super Moderator
    Points: 236,290, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    40,849
    Helped
    12488 / 12488
    Points
    236,290
    Level
    100

    Re: Structure value assign to an array

    The purpose of the bitwise or operation isn't clear. You are merging four 4-bit values into one. In the assumed special cased, the operation can be reversed, but generally it can't.

    Presumed the bitwise or is what you want, it has to be actually performed during the "assignment". There's no way to simplify the syntax, except for using macros or functions.



    •   Alt11th November 2017, 13:23

      advertising

        
       

  4. #4
    Member level 5
    Points: 1,626, Level: 9

    Join Date
    Nov 2010
    Posts
    82
    Helped
    0 / 0
    Points
    1,626
    Level
    9

    Re: Structure value assign to an array

    Actually i have made it wrong.
    what i want is
    let suppose i have an 8bit variable, i only required its 4bits from LSB
    and to access these bit's i need a structure.
    for example if i need a value (0 or 1) to load on 1st bit, i write something like that A0.position1 = 1;
    if i need to load a value(0 or 1) at 3rd bit , i write A0.position3 = 0;

    How to do that?
    or Is there anyother way to do that?
    Hope i explained it properly



  5. #5
    Member level 5
    Points: 1,626, Level: 9

    Join Date
    Nov 2010
    Posts
    82
    Helped
    0 / 0
    Points
    1,626
    Level
    9

    Re: Structure value assign to an array

    guys i want this
    Click image for larger version. 

Name:	IMG_20171114_170314.jpg 
Views:	1 
Size:	494.8 KB 
ID:	142513



    •   Alt14th November 2017, 13:30

      advertising

        
       

  6. #6
    Super Moderator
    Points: 236,290, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    40,849
    Helped
    12488 / 12488
    Points
    236,290
    Level
    100

    Re: Structure value assign to an array

    Usually you'll define a union of 4 single bit variables and a character to access all bits together.

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    union
    {
        char val;
        struct
        {
            char b0:1;
            char b1:1;
            char b2:1;
            char b3:1;
        } bits;
    } a;
     
    // access example
    a.bits.b0 = 1;
    a.bits.b1 = 0;
    result = a.val;


    1 members found this post helpful.

--[[ ]]--