WAP to print a 2 dim array using pointer notation

1st method
#include<stdlib.h>
 #include<stdio.h>

 main()
 {
 int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
 int i,j;
for(i=0;i<3;i++)
 {
 for(j=0;j<4;j++)
 {
 printf("%d", *(*(a+i)+j));
 }}
 system("PAUSE");
 }
method-2
#include<stdlib.h>
 #include<stdio.h>
int main()
 {
 int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
 int i,j;
 int *p;
 p=&a[0][0];
 for(i=0;i<3;i++)
 {
 for(j=0;j<4;j++)
 {
 printf("%d",*(p+i*4+j));
 }
 printf("\n");
 }
 system("PAUSE");
 }
method-3
 #include<stdlib.h>
 #include<stdio.h>
int main()
 {
 int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
 int i,j;
 int *p;
for(i=0;i<3;i++)
 {
 p=&a[i][0];
for(j=0;j<4;j++)
 {
 printf("%d",*(p+j));
 }
 printf("\n");
 }
 system("PAUSE");
 }
Advertisements

Leave a comment

Filed under TUTORIALS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s