Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Structure value assign to an array

Status
Not open for further replies.

love_electronic

Member level 5
Member level 5
Joined
Nov 16, 2010
Messages
93
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,964
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.
 

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.
 

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.
 

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
 

guys i want this
IMG_20171114_170314.jpg
 

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;

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top