pass array of different sizes as argument

This is a discussion on pass array of different sizes as argument within the C Programming forums, part of the General Programming Boards category; I have multiple print functions that operate on multidimensional arrays of different sizes. I would like to write a function ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    pass array of different sizes as argument

    I have multiple print functions that operate on multidimensional arrays of different sizes.
    I would like to write a function masterPrint that would allow me to pass an array to it with rowsize and columnsize. I will be monitoring this post for most the day if you have any further questions I can answer

    something like this
    Code:
    void masterPrint(rowsize,columnsize,arrayname)
    {
         
    }
    
    example of use
    masterPrint(4,4,matrix);
    masterPrint(3,4,dMatrix);
    Code:
    void matrixPrint()
    {
    	char i=0;
    	char j=0;
    	
    	
    	printf( "matrix print:\n");
    		for (i=0; i < rowend; i++) 
    		{
    			printf( "row %d=", i);
    			for (j=0; j < columnend; j++) 
    			{
    				printf("[%d]", matrix[i][j]);
          
    			} 
    			printf("\n"); 
    		}
    }
    
    void DependenceMatrixPrint()
    {
    	char i=0;
    	char j=0;		
    	printf( "\nDependence print:\n");
    		for (i=0; i < 4; i++) 
    		{
    			printf( "row %d=", i);
    			for (j=0; j < 3; j++) 
    			{
    				printf("[%d]", D[i][j]);
          
    			} 
    			printf("\n"); 
    		}
    }
    
    void printReachable()
    {
    	char i;
    	char j;
    		for (i=0; i < rowend; i++) 
    		{
    			printf( "reachable %d=", i);
    			printf("[%d]", reachable[i]);
    			printf("\n");
    		}
    }
    
    void printDependence()
    {
    	char i;
    	char j;
    		for (i=0; i < rowend; i++) 
    		{
    			printf( "dependence %d=", i);
    			printf("[%d]", dependence[i]);
    			printf("\n");
    		}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hacknItTogether View Post
    I will be monitoring this post for most the day if you have any further questions I can answer
    Yes, is your name a Dr. Hook reference by any chance?

    Also, do you know how to use a pointer-to-a-pointer?

    Code:
    char **matrix;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    yes I have used a pointer to a pointer

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hacknItTogether View Post
    yes I have used a pointer to a pointer
    That is what you want to use as the third parameter to masterPrint. Getting that to work with a matrix declared like this:
    Code:
    char matrix[6][6];
    is a bit of a hassle. After that, it is pretty straight-forward.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    ouch, all my matrixes are char matrix[6][6]; that'd be a large code rewrite bad choice on my part but something to be learned. I was previously doing alot of C++ but switched to C for a different project it has taught me the niceness of C++. No sorry not a Dr.Hook reference, my boss always tells me don't hack it together no hacking, i'm infamous for grabbing portions of code throwing it into place just to get the job done
    Last edited by hacknItTogether; 09-24-2009 at 01:21 PM.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Yep! you can pass arrays of different sizes to masterPrint() and call it like
    Code:
    masterPrint(4, 4, matrix);
    masterPrint(6, 6, dmatrix);
    So what's the question

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    the arrays are defined at compile time and already exist. for example I have
    DependenceMatrix[4][4]
    ReachableMatrix[1][3]
    MatrixPermutation[4][4]

    I want
    Code:
    #include <stdio.h>
    void matrixInit(char rowend,char columnend);
    char Matrix[4][4];
    void masterPrint(char rowend,char columnend, char matrixToBePassed);
    int main()
    {	
    	matrixInit(4,4);
    	masterPrint(4,4,Matrix);
    }
    void matrixInit(char rowend,char columnend)  
    {
    	char i=0;
    	char j=0;
    	for (i = 0; i < rowend; i++) 
    	{
    		for (j = 0; j < columnend; j++) 
    		{
    			matrix[i][j] = i + 1;                         
    		}   
    	}
    }
    void masterPrint(char rowend,char columnend, char matrixToBePassed)
    {
    	char i=0;
    	char j=0;
    	printf( "matrix print:\n");
    		for (i=0; i < rowend; i++) 
    		{
    			printf( "row %d=", i);
    
    			for (j=0; j < columnend; j++) 
    			{
    				printf("[%d]", matrixToBePassed[i][j]);
          
    			} 
    			printf("\n"); 
    		}
    }
    Last edited by hacknItTogether; 09-24-2009 at 01:37 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the arrays are already defined as 2-D arrays, then they are laid out contiguously. I.e. your dependence matrix is laid out as sixteen consecutive chars, by row. So you can make your third parameter a simple char*, and then walk through the array character by character (you know where the line breaks are, since the sizes were passed in).

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Array dimensions don't matter as long as it's cast to the correct type
    Code:
    void masterPrint(int rowsize, int columnsize, char *a)
    {
        printf("%s\n", a);
    }
    
    int main()
    {
        char m[4][4];
        char a[6][6];
    
        masterPrint(4, 4, (char *) m);
        masterPrint(6, 6, (char *) a);
    }

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    what your saying sounds good tabstop but I don't quite understand so your saying do something like
    Code:
    void masterPrint(char rowend,char columnend, char *matrixToBePassed)
    {
    	char i=0;
    	char j=0;
    	printf( "matrix print:\n");
    		for (i=0; i < rowend; i++) 
    		{
    			printf( "row %d=", i);
    
    			for (j=0; j < columnend; j++) 
    			{
    				printf("[%d]", *matrixToBePassed[i][j]);
          
    			} 
    			printf("\n"); 
    		}
    }
    does the pointer point to the array?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hacknItTogether View Post
    ouch, all my matrixes are char matrix[6][6]; that'd be a large code rewrite bad choice on my part but something to be learned. I was previously doing alot of C++ but switched to C for a different project it has taught me the niceness of C++.
    Oh fluff -- don't listen to that tish. You do not have to do a major rewrite, you just need a couple of lines of code to assign a **ptp to your matrix, look:
    Code:
    #include <stdio.h>
    
    void func(int x, int y, int **ptr) {
    	int i, j;
    	for (i=0;i<x;i++) {
    		for (j=0;j<x;j++) printf("%d ",ptr[i][j]);
    		printf("\n");
    	}
    }
    
    int main(void) {
    	int ray[3][3] = { {1,2,3},{4,5,6},{7,8,9} }, **ptr, i;
            /* whoops! I didn't initialize ptr, this code is bad */
    	for (i=0; i<3; i++) ptr[i] = ray[i];
    	func(3,3,ptr);
    	return 0;
    }
    Those lines are in red. In C, we call this a hassle, but nothing to sweat n' fret over. In C++ maybe you throw up your hands and look for the HelpMe::Library, I dunno

    Actually I'm gonna check out what tabstop is implying here...[later] nah that won't work.
    [edit] Yes it will. Also the above has an overwrite in it...as noted in red
    Last edited by MK27; 09-24-2009 at 02:57 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What I'm saying is that if matrixToBePassed is a char*, then *matrixToBePassed is a char, specifically the first character in the array, and we know how to print chars. Also matrixToBePassed+1 would then point to the next character, and matrixToBePassed+2 would then point to the character after that, and matrixToBePassed+3 would then point to the character after that....

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by hacknItTogether View Post
    does the pointer point to the array?
    Yep! it will if you cast it properly - see my last post.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    What I'm saying is that if matrixToBePassed is a char*, then *matrixToBePassed is a char, specifically the first character in the array, and we know how to print chars. Also matrixToBePassed+1 would then point to the next character, and matrixToBePassed+2 would then point to the character after that, and matrixToBePassed+3 would then point to the character after that....
    Sure, and you could just use pointer arithmetic in masterPrint based on x & y. But this strategy will not work with 2D arrays declared matrix[x][x], unless you'd like to enlighten us with some code...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    I will try to implement yours MK

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

Similar Threads

  1. How to pass in an array/struct as argument
    By Tom_Arch in forum C Programming
    Replies: 11
    Last Post: 04-23-2009, 05:16 PM
  2. Using INT array to pass structures?
    By HyperShadow in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 11
    Last Post: 09-22-2006, 06:21 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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