Retrieve Data from a Two-Dimensional Array Using an Array Name using three different approaches - C Array

C examples for Array:Multidimensional Arrays

Description

Retrieve Data from a Two-Dimensional Array Using an Array Name using three different approaches

Demo Code

#include <stdio.h>

int main()/*from  w  w  w  .ja  va2s . c o  m*/
{
     int *p1, *p2, *p3;
     int t1 [][4] = {
                                    {1, 4, 7, 8},
                                    {2, 5, 8, 8},
                                    {3, 6, 9, 8}
                               };
     int t2 [3][4] = {
                                    {3, 5, 7, 9},
                                    {3, 5, 7, 9},
                                    {3, 5, 7, 9}
                                 };
    
     int t3 [3][4];
    
     printf("\nTable t3 is computed and displayed:\n\n");
    
     for(int i = 0; i < 3; i++) {
          for(int j = 0; j < 4; j++) {
                *(t3[i] + j) = *(t1[i] + j) + *(t2[i] + j);
                 printf("%d  ", *(t3[i] + j));
          }
          printf("\n");
     }
    
     printf("\n\nTable t3 is computed and displayed again:\n\n");
    
     for(int i = 0; i < 3; i++) {
          for(int j = 0; j < 4; j++) {
               *(*(t3 + i) + j) = *(*(t1 + i) + j) + *(*(t2 + i) + j);
               printf("%d  ", t3[i][j]);
          }
          printf("\n");
     }
    
     p1 = (int *) t1;
     p2 = (int *) t2;
     p3 = (int *) t3;
     printf("\n\nTable t3 is computed and displayed again:\n\n");
    
     for(int i = 0; i < 3; i++) {
          for(int j = 0; j < 4; j++) {
               *(p3 + i * 4 + j) = *(p1 + i * 4 + j) + *(p2 + i * 4 + j);
                printf("%d  ", *(p3 + i * 4 + j));
          }
          printf("\n");
     }
    
     return(0);
}

Result


Related Tutorials