Thread: multi string arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    30

    multi string arrays

    Hi

    If I have:

    Code:
    	char bit_pattern[][4][4] = {
    		"0000", "0001", "0010", "0011",
    		"0100", "0101", "0110", "0111",
    		"1000", "1001", "1010", "1011",
    		"1100", "1101", "1110", "1111"};
    How do I extract an element/string i.e "0000", "0001" etc

    Thanks

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    How do I extract an element/string
    By the first two 'coordinates'.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    I tried that, when I do:

    Code:
    printf("\n\nbit_pattern: %s\n\n", bit_pattern[1][1]);
    I get: bit_pattern: 01010110011110001001101010111100110111101111, which is basically some combination of the elements.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nasser View Post
    Hi

    If I have:

    Code:
    	char bit_pattern[][4][4] = {
    		"0000", "0001", "0010", "0011",
    		"0100", "0101", "0110", "0111",
    		"1000", "1001", "1010", "1011",
    		"1100", "1101", "1110", "1111"};
    How do I extract an element/string i.e "0000", "0001" etc

    Thanks
    As manasij points out ...
    Code:
    printf("%s", bit_pattern[i][j]);
    where i and j are the first two indexes of your array.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nasser View Post
    I tried that, when I do:

    Code:
    printf("\n\nbit_pattern: %s\n\n", bit_pattern[1][1]);
    I get: bit_pattern: 01010110011110001001101010111100110111101111, which is basically some combination of the elements.
    Depending on your compiler, you may need to change your intializer...
    Code:
    	char bit_pattern[4][4][] = {
    		"0000", "0001", "0010", "0011",
    		"0100", "0101", "0110", "0111",
    		"1000", "1001", "1010", "1011",
    		"1100", "1101", "1110", "1111"};
    or

    Code:
    	char bit_pattern[4][4][] = {
    		{"0000", "0001", "0010", "0011"},
    		{"0100", "0101", "0110", "0111"},
    		{"1000", "1001", "1010", "1011"},
    		{"1100", "1101", "1110", "1111"}
                             };

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Shouldn't
    Code:
    char bit_pattern[4][4][]
    be
    Code:
    char bit_pattern[][4][5]
    or
    Code:
    char bit_pattern[4][4][5]
    Jim

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by nasser View Post
    I tried that, when I do:

    Code:
    printf("\n\nbit_pattern: %s\n\n", bit_pattern[1][1]);
    I get: bit_pattern: 01010110011110001001101010111100110111101111, which is basically some combination of the elements.
    Try something like:
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	char myarray[][4][5]={{"0000","0001","0010","0011"},
    	{"0100","0101","0110","0111"},
    	{"1000","1001","1010","1100"},
    	{"1101","1110","1111","0000"}};
    
    	for(int x=0;x<4;x++)
    		for(int y=0;y<4;y++)
    			printf("%s\n",myarray[x][y]);
    
    	getchar();
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    Shouldn't
    Code:
    char bit_pattern[4][4][]
    be
    Code:
    char bit_pattern[][4][5]
    or
    Code:
    char bit_pattern[4][4][5]
    Jim
    This is something that, from experimenting with different compilers, I interpret as "implementation dependent"... while the last one is almost certainly correct... the others may not be. (In my experience... not per any known standard.)

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    AndrewHunters suggestion worked, thanks.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    One more question, how can I assign a pointers value into an array i.e

    where printArray is a pointer to a char got from a function. This doesn't seem to work:

    Code:
    for(i=0; i<10;i++) {
    
    empty[i] =  *(printArray+i);
    
    }
    But I can print the individual characters of *(printArray + i) and get the right characters just fine. thanks

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Clearly this is for that hex to binary conversion again, in which case the dimensionality of the array should be reduced by one. Make it char bit_pattern[16][5]
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multi-dimensional arrays
    By shuo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2008, 10:22 PM
  2. multi-dimensional arrays
    By maple23 in forum C Programming
    Replies: 4
    Last Post: 05-31-2008, 01:58 AM
  3. Multi-Arrays
    By KoG Metalgod in forum C Programming
    Replies: 7
    Last Post: 12-06-2006, 09:56 AM
  4. Help with multi-dimensional string arrays
    By nsaP in forum C Programming
    Replies: 9
    Last Post: 12-13-2004, 08:17 PM
  5. multi-dimensional arrays
    By abrege in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 05:09 PM