2d Array and pointers

User avatar
arunbm123
Posts: 96
Joined: Fri Feb 23, 2018 5:36 am

2d Array and pointers

Postby arunbm123 » Tue Jun 04, 2019 10:20 am

hello friends,

please find a simple test Code.
I defined 2D array and passing it to show function. I am not understanding why I am getting Warning..
Anyone kindly enlighten me...

warning: passing argument 1 of 'show' from incompatible pointer type
note: expected 'int *' but argument is of type 'int (*)[5][2]

Code: Select all


		int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
		int i,m,n,*j;
		m=5;
		n=2;
		show(arr,m,n);
		
		
		void show (int *b,int l,int p)
		{
			 int i,j;
			 printf("show \n");
			 for(i=0;i<l;i++){
                        printf("%d %d \n",*(b+i*2+0),*(b+i*2+1));
             		}
		 }

		

DrScanlon
Posts: 11
Joined: Tue Oct 04, 2016 8:46 pm

Re: 2d Array and pointers

Postby DrScanlon » Tue Jun 04, 2019 3:43 pm

I'm assuming C code. Something like the following should work:

Code: Select all

        int arr[][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
        int i,m,n,*j;
//      m=5;
//      n=2;
//      show(arr,m,n);

        void show (int *b,int l,int p)
        {
                 int i,j;
                 printf("show \n");
                 for(i=0;i<l;i++){
                       printf("%d %d \n",*(b+i*2+0),*(b+i*2+1));
                        }
        }

        void main() {
                m = 5;
                n = 2;
// 2D array pointer
               int *arptr = &arr[0][0];
               show(arptr,m,n);
        }

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: 2d Array and pointers

Postby fly135 » Tue Jun 04, 2019 3:51 pm

I'm doing this off the top of my head, but it should work..
typedef int arr2d[2];

arr2d myvar[] = {{11,12},{21,22},{31,32},{41,42},{51,52}};

or...

arr2d *myvar = {{11,12},{21,22},{31,32},{41,42},{51,52}};


void show (myvar *b,int l,int p);

Who is online

Users browsing this forum: No registered users and 257 guests