pass a 2d array to function which takes an array of pointers as argument?

Discussion in 'C' started by algafan, Oct 21, 2007.

  1. algafan

    algafan New Member

    Joined:
    Oct 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    So what I want to do is something like that:
    Code:
    void function(char *ar[]){
            strcpy(ar[0],"blablabla");
    }
    int main(){
            char str[100][200];
            function(str);         //wrong

    I mean I want to have a function to which i can pass a 2d array and it ll make some changes to it. I do not want a function with a 2d array argument cause i do not want to set the size of array explicitly, so I believe this is the only way. The thing is that

    function(str);

    is wrong and I get a warning during compile:

    test.c:9: warning: passing argument 1 of function from incompatible pointer type

    so what I 'm asking is, what is the right way to call function on this array or/and if there is another way to do all this.
    thanks.
     
  2. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    A good example would be matrix operations
    i have made function's
    input-for taking the matrix from user
    output-for displaying dsired matrix
    addtn-for adding matrices
    subtn-subtracting
    multn-multiplying

    Code:
    #include<stdio.h>
    #include<conio.h>
    void input(int (*in)[3]);
    void output(int (*out)[3]);
    void addtn(int (*x)[3],int (*y)[3]);
    void subtn(int (*x)[3],int (*y)[3]);
    void multi(int (*x)[3],int (*y)[3]);
    void main()
    {
    int mat1[3][3],mat2[3][3],choice;
    int i,j;
    clrscr();
    
    printf("\nEnter the elements of first matrix:\n");
    input(mat1);
    printf("\nEnter the elements of second matrix:\n");
    input(mat2);
    
    do
    	{
    	clrscr();
    	printf("\t\t\t 1) ADDITION\n");
    	printf("\t\t\t 2) SUBTRACTION\n");
    	printf("\t\t\t 3) MULTIPLICATION\n");
    	printf("\t\t\t 4) EXIT\n\n\n");
    	printf("\n\nEnter Your choice: ");
    	scanf("%d",&choice);
    		switch(choice)
    		{
    		case 1:
    			addtn(mat1,mat2);
    			break;
    		case 2:
    			subtn(mat1,mat2);
    			break;
    		case 3:
    			multi(mat1,mat2);
    			break;
    		case 4:
    			exit(1);
    		default :
    			printf("Not in the list of choices. Enter from the list only!");
    		}
    
    }while(1);
    }
    //********************************INPUT***************************************
    void input(int (*in)[3])
    {
    int i,j;
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    	{
    		printf("\nEnter the Element of row,%d and col,%d: ",i+1,j+1);
    		scanf("%d",(*(in+i)+j));
    	}
    }
    getch();
    clrscr();
    }
    //********************************DISPAY MATRIX******************************
    void output(int (*out)[3])
    {
    int i,j;
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    	{
    	printf("%d\t",*(*(out+i)+j));
    	}
    printf("\n");
    }
    }
    //*******************************ADDITION*************************************
    void addtn(int (*x)[3],int (*y)[3])
    {
    int i,j,c[3][3];
    for(i=0;i<3;i++)
    	for(j=0;j<3;j++)
    		c[i][j]=0;
    
    printf("\nThe 1st matrix Entered is:\n");
    output(x);
    printf("\nThe 2nd matrix Entered is:\n");
    output(y);
    
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    	{
    		c[i][j]= *(*(x+i)+j)+ *(*(y+i)+j);
    	}
    }
    printf("\nThe resultant sum matrix is:\n");
    output(c);
    
    getch();
    clrscr();
    }
    
    //**************************** subtraction ******************************
    void subtn(int (*x)[3],int (*y)[3])
    {
    int i,j,c[3][3];
    for(i=0;i<3;i++)
    	for(j=0;j<3;j++)
    		c[i][j]=0;
    printf("\nThe 1st matrix Entered is:\n");
    output(x);
    printf("\nThe 2nd matrix Eneterd is:\n");
    output(y);
    for(i=0;i<3;i++)
    		for(j=0;j<3;j++)
    		{
    			c[i][j]=*(*(x+i)+j)-*(*(y+i)+j);
    		}
    printf("\nThe resultant Difference matrix is:\n");
    output(c);
    getch();
    clrscr();
    }
    //**************************MULTIPLICATION***********************************
    void multi(int (*x)[3],int (*y)[3])
    {
    int i,j,k,c[3][3];
    for(i=0;i<3;i++)
    	for(j=0;j<3;j++)
    		c[i][j]=0;
    
    printf("\nThe 1st matrix Entered by you is:\n");
    output(x);
    printf("\nThe 2nd matrix Entered by you is:\n");
    output(y);
    
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    		for(k=0;k<3;k++)
    		{
    		c[i][j]+=(*(*(x+i)+k))*(*(*(y+k)+j));
    		}
    }
    printf("\nThe resultant Product matrix is:\n");
    output(c);
    getch();
    clrscr();
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice