Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
#include "stdio.h"

void main(){
 int a[2][2]={1, 2, 3, 4};
 int a[2][2]={1, 2, 3, 4};
 display(a, 2, 2);
 show(a, 2, 2);}
}

display(int *k, int r, int c){
int i, j, *z;
 for(i = 0; i < r; i++){
   z = k + i;
   printf("Display\n");
      for(j = 0; j < c; j++){
          printf("%d", *(z + j));
       }
  }
}

show(int *q, int ro, int co){
int i, j;
   for(i = 0; i < ro; i++){
     printf("\n");
     for(j = 0; j < co; j++){
       printf("%d", *(q + i*co + j));
     }
   }
}

Output:

Display
12
23
Show
12
34

Why Display() is not showing 1223 while show() gives 1234? Both uses the same logic to display the 2d array. Can any one help please?

share|improve this question
3  
first, put our code in a readable format! –  philippe Jul 15 '12 at 15:06
1  
Please invest the small amount of time required to figure out how to indent your code properly. Nobody here wants to read that mess. –  meagar Jul 15 '12 at 15:07
    
If you're too lazy, at least run indent -kr on your file before copypasting it... –  user529758 Jul 15 '12 at 15:09
    
now its fine...?? –  user1604151 Jul 15 '12 at 15:14
add comment

1 Answer

In display you are using two counters, i for rows and j for columns. Since the array is laid out sequentially in memory you need to increase i by the size of a column, i.e. c, each time you want to move from one row to the next. So, you should add i*c to k, not i.

The complete function:

display(int *k,int r,int c){
int i,j,*z;
 for(i=0;i<r;i++){
   z=k+i*c;
   printf("Display\n");
      for(j=0;j<c;j++){
          printf("%d",*(z+j));
       }
  }
}
share|improve this answer
    
Thank You very much...:) –  user1604151 Jul 15 '12 at 15:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.