Arrays in C (2D Array) – Set 6

As I revealed to you before the idea of exhibits is firmly identified with pointers. Clusters will work quicker by utilizing pointers instead of utilizing a subscript.

So it is constantly prescribed to access exhibits by utilizing pointers. In the last instructional exercise, I gave a short diagram on 2D exhibits in C. Today I will talk about the utilization of 2D exhibits with pointers. This is one of the most mind-boggling themes of C programming, so I might want to propose you to peruse this instructional exercise with full focus.

2D Arrays with Pointers

A 2D exhibit is only a mix of 1D clusters. To demonstrate my point I might want to show you a model.

#include<stdio.h>
 
int main( )
{
 int s[4][2]={
   {542,43},
   {154,354},
   {432,54},
   {435,435}
   };
 int x;
 
 for(x=0;x<=3;x++)
  printf("n Address of %d th 1-D array = %u",x,s[x]);
 
 return 0;
 
}

Output

Address of 0 th 1-D array = 65510 
Address of 1 th 1-D array = 65514 
Address of 2 th 1-D array = 65518 
Address of 3 th 1-D array = 65522 

Clarification

In the first place, I have announced a 2D exhibit with 4 lines and 2 sections and furthermore introduced it. So we can say that this 2D cluster is a gathering of four 1D exhibits having 2 components each.

After that, I have pronounced the whole number variable x which will go about like a circle counter.

Presently I have begun the for a circle with one printf() work. Consider cautiously the contentions in printf() work. I have given two contentions which are x and s[x].

As should be obvious I am just getting to the 2D cluster with one measurement. So it will give the addresses of just 1D clusters. s[0] will show the location of the first component of the first 1D exhibit, s[1] will show the address of the first component of the second 1D cluster, etc.

Access 2D Array Using Pointer Notation

The most ideal approach to learn it is through a program.

#include<stdio.h>
 
int main()
{
 int s[4][2]={
   { 542, 43 },
   { 154, 354 },
   { 432, 54 },
   { 435, 435 }
   } ;
 int x;
 
  printf(" %dn",s[2][1]);
 printf(" It will give you address of 1st element of 3rd 1D array - %u n",s[2]);
 printf(" It will give you address of 2st element of 3rd 1D array - %u n",s[2]+1);
 printf(" Value at that address %d n", *(s[2]+1));
 printf(" Alternate way of accessing that address %d n",*(*(s+2)+1));
 
  return 0;
}

Output

54 
It will give you address of 1st element of 3rd 1D array - 65518 
It will give you address of 2st element of 3rd 1D array - 65520 
Value at that address 54 
Alternate way of accessing that address 54

Leave a Comment

error: Alert: Content is protected!!