# Array inside an 2dimensional array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-22-2008
aserf
Array inside an 2dimensional array
First of all I would like to apologize if my question has been answered in the past...

I want to create a 30 columns *2 rows array (A[30][2]) and store at every element of it another array.

Practically, I want to store 8 bit binary addresses in a 2dimensional array, however I want these addresses to be stored as arrays and not as integers as I want to be able to read them byte by byte.

Therefore
Code:

`int A[30][2];`
as declaration is not what I want.

What I want is to store
Code:

`B[8]={0,0,0,0,1,1,1,1}, C[8]=..., etc at every element of A[30][2].`
If somebody could help me I would be grateful!!!

Thank you very much in advance.
• 03-22-2008
mike_g
Maybe a 3 dimensional array? Or have I just missed your point?
• 03-22-2008
aserf
Quote:

Originally Posted by mike_g
Maybe a 3 dimensional array? Or have I just missed your point?

Yes, you 're right, I didn' t explain well what I want...:(

If I declare an
Code:

`int A[30][2][8];`
array I will have to fill it up like this:

Code:

```A[0][0][0] = 0; A[0][0][1] = 0; A[0][0][2] = 0; A[0][0][3] = 0; A[0][0][4] = 1; A[0][0][5] = 1; A[0][0][6] = 1; A[0][0][7] = 1;```
which represents the first byte 11110000 (from MSB to LSB).

I will have to repeat this 30*2 = 60 times.

Could you please tell how I could put in every element of the 30*2 array directly a full byte as an array?
How can I just declare
Code:

`B[8] = {0,0,0,0,1,1,1,1};`
and then just put it inside A[0][0]?

Thank you again for your time...
• 03-22-2008
Dave_Sinkula
Code:

```unsigned char array[2][30]; array[0][0] = 0xF0;```
?
• 03-22-2008
IceDane
Use a union and a struct with bitfields:

Code:

``` union {     unsigned char thebyte;     struct     {         unsigned int bit1:1;         unsigned int bit2:1;         unsigned int bit3:1;         unsigned int bit4:1;         unsigned int bit5:1;         unsigned int bit6:1;         unsigned int bit7:1;         unsigned int bit8:1;     }bits; }byte; int main(void) {     byte.thebyte = 0;     byte.bits.bit8 = 1;  // equivalent to 1000 0000 (msb to lsb order)     printf("Byte is: %d\n", byte.thebyte);     return 0; }```
• 03-22-2008
Dave_Sinkula
Quote:

Originally Posted by IceDane
Use a union and a struct with bitfields:

The approach has portability issues. I prefer plain old bytes.
• 03-22-2008
aserf
Quote:

Originally Posted by Dave_Sinkula
Code:

```unsigned char array[2][30]; array[0][0] = 0xF0;```
?

This would be just fine if I just wanted to be able to write a full 8 bit binary at once and read it at once. However, I mentioned that I' ll have to be able to read the data bit by bit as I will send them through a serial port to a external hardware device bit by bit . So it is very helpful for me that each bit is an array element.

p.s. I' m sorry for the "stupid" questions, however I' m much more familiar with hardware programming than with software programming.
• 03-22-2008
mike_g
Maybe you could use bitwise operators to filter out the bits in the byte you want?

Untested, but maybe something like:
Code:

```int GetBit(unsigned char byte, unsigned char bit) {     bit = 1 << bit;     return (byte & bit); } int main() {     unsigned char byte = 0xFF;     int state = GetBit(byte, 4); }```
• 03-22-2008
Dave_Sinkula
Quote:

Originally Posted by aserf
This would be just fine if I just wanted to be able to write a full 8 bit binary at once and read it at once. However, I mentioned that I' ll have to be able to read the data bit by bit as I will send them through a serial port to a external hardware device bit by bit . So it is very helpful for me that each bit is an array element.

This serial port doesn't use a byte-sized shift register like every other CPU under the sun?
• 03-22-2008
aserf
Quote:

Originally Posted by mike_g
Maybe you could use bitwise operators to filter out the bits in the byte you want?

Untested, but maybe something like:
Code:

```int GetBit(unsigned char byte, unsigned char bit) {     bit = 1 << bit;     return (byte & bit); } int main() {     unsigned char byte = 0xFF;     int state = GetBit(byte, 4); }```

This seems practical. I will try it and give you the feedback in a while.

Thank you for your time and patience...
• 03-22-2008
aserf
Quote:

Originally Posted by Dave_Sinkula
This serial port doesn't use a byte-sized shift register like every other CPU under the sun?

I wish it did... it would have made my work much easier.

It' s a Nios II Stratix II board (for those who know) of which I can control the I/O pins. Therefore I have to create a program that will work as hardware does.
Specifically, I use the one pin as a clock (practically by constantly changing its output from 0 to 1) and in the same time I use the other pin to send bit by bit the data synchronized with the clock. That' s why I can' t send the whole byte at once, as I'm not using an existing hardware.
• 03-22-2008
aserf
@Dave_Sinkula

I used this:

Code:

```#include "stdafx.h" #include <stdio.h> #include <windows.h> #include "math.h" int GetBit(unsigned char byte, unsigned char bit) {     bit = 1 << bit;     return (byte & bit); } int _tmain(int argc, _TCHAR* argv[]) {         unsigned char byte = 0x2B;     int i;                 for (i=7;i>-1;i--)         {                 int state = GetBit(byte, i);                         if (state == 0)                 {                         printf("%d",state);                 }                 else                 {                         state = state/(pow(2,i));                         printf("%d",state);                 }         }         for(;;)         {         } }```
to test it and it' s just what I wanted! Now I can use a 2 dimension array with unsigned char elements.
Code:

`state = state/(pow(2,i));`
because without this state had as a value the power of 2 of the location of the bit.

You have been really helpful :D
• 03-22-2008
mike_g
If you want the get bit function to return 0 or 1 you could use:
Code:

```int GetBit(unsigned char byte, unsigned char bit) {     bit = 1 << bit;     if(byte & bit) return 1;     return 0; }```
It would save using an expensive pow function ;)
• 03-22-2008
aserf
Quote:

Originally Posted by mike_g
If you want the get bit function to return 0 or 1 you could use:
Code:

```int GetBit(unsigned char byte, unsigned char bit) {     bit = 1 << bit;     if(byte & bit) return 1;     return 0; }```
It would save using an expensive pow function ;)

True!!!

I made the change and it works just fine!

Thank you all! You have been really helpful!!!
• 03-22-2008
vart
Quote:

If you want the get bit function to return 0 or 1 you could use:
or maybe just
Code:

`return (byte>>bit) & 1;`
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last