passing multidimensional array

This is a discussion on passing multidimensional array within the C Programming forums, part of the General Programming Boards category; hello, this program is supposed to convert an array with dec values to binary. it compiles without errors but then ...

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    passing multidimensional array

    hello,
    this program is supposed to convert an array with dec values to binary. it compiles without errors but then it gives a segfault:
    Code:
    #include <stdio.h>
    
    void dectobin (int *dec, int **bit);
    
    int main( void ) {
    
    int dec[8] = { 12,13,14,15,16,17,18,19 } ;
    int **bit ;
    
    dectobin (dec, bit);
    
    return 0;
    }
    
    void dectobin (int *dec, int **bit) {
    
     int j,i;
       
     for( i = 0; i < 8; i++ ) {
    	for(j=7;j>=0;j--) 
          {
    	bit[i][j] = (dec[i] % 2);
    	dec[i] /=2;
    	}
          }
          
     for( i = 0 ; i < 8; i++ ) {
     
    	printf("\n");
    	
    	for(j=0; j<8; j++)
    	printf("%d",bit[i][j]);
    	
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This: "int **bit;" is not a multi dimension array. It is a pointer to a pointer, which has nothing allocated for it. It's just a pointer. Nothing more. You have to allocate space for each pointer, and then for what each pointer points to for it to be correct.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Or just make it an array to begin with

    Code:
    #include <stdio.h>
    
    void dectobin (int dec[], int bit[][8]);
    
    int main( void ) {
    
    int dec[8] = { 12,13,14,15,16,17,18,19 } ;
    int bit[8][8] ;
    
    dectobin (dec, bit);
    
    return 0;
    }
    
    void dectobin (int dec[], int bit[][8]) {
    
     int j,i;
       
     for( i = 0; i < 8; i++ ) {
    	for(j=7;j>=0;j--) 
          {
    	bit[i][j] = (dec[i] % 2);
    	dec[i] /=2;
    	}
          }
          
     for( i = 0 ; i < 8; i++ ) {
     
    	printf("\n");
    	
    	for(j=0; j<8; j++)
    	printf("%d",bit[i][j]);
    	
    	}
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thank you , it works well , although i still have a question . If i just pass the bit[][] to the function it will say :
    Code:
    invalid use of array with unspecified bounds
    That means i have to pass the array with specified bounds like bit[][8] ? Why is that? why doesn't bit[][] work?

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    It doesn't work because of how the compiler and computer give memory to the array. The 2-d array is nothing more than a group of 1d arrays one after the other. It needs a number in the second bracket to know how long to make each segment. (Hope that made sense). If you don't want to have to worry about the size of your array, just malloc() it instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Multidimensional Array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2001, 05:18 PM

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