Array inside an 2dimensional array

This is a discussion on Array inside an 2dimensional array within the C Programming forums, part of the General Programming Boards category; First of all I would like to apologize if my question has been answered in the past... I want to ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    8

    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.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Maybe a 3 dimensional array? Or have I just missed your point?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by mike_g View Post
    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...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    unsigned char array[2][30];
    array[0][0] = 0xF0;
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    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;
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by IceDane View Post
    Use a union and a struct with bitfields:
    The approach has portability issues. I prefer plain old bytes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by Dave_Sinkula View Post
    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.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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);
    }
    Last edited by mike_g; 03-22-2008 at 02:22 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by aserf View Post
    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?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by mike_g View Post
    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...

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by Dave_Sinkula View Post
    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.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    @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.
    I just had to add
    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

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by mike_g View Post
    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!!!

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you want the get bit function to return 0 or 1 you could use:
    or maybe just
    Code:
    return (byte>>bit) & 1;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing an array inside of a structure
    By Lima in forum C Programming
    Replies: 6
    Last Post: 06-08-2009, 09:53 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. looking inside of an array
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2002, 12:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21