简体   繁体   中英

2D array…Printing extra elements?

I have this following program. Where i am unable to understand what is happening. It prints one extra element, i must been a dump person? couldn't make it why? Please help me!

 #include <stdio.h>

 int main(){
         int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};
         int i = 0, j=0;
         int *ptr = &n;

         for(i=0; i<3; i++)
             for(j=0; j<9; j++)
         {
            printf("%d\t",*((ptr+i)+j)) ;
         }
         system("pause");
         return 0;
  }

This is not at all how you access elements of a 2D array. There are several things wrong here. For one, you are looping over both i and j - but since you are taking j from 0 to 8 , you will run into trouble when you hit a value i>0. You could just do

#include <stdio.h>

int main(){

  int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};

  int j=0;   
  int *ptr = &n[0][0];

  for(j=0; j<9; j++)
  {
     printf("%d\t",*(ptr+j)) ;
  }

  return 0; 
}

It is a bit simpler, but maintains the essence of your code - in that it is setting a pointer to the start of the array, and incrementing this pointer. Some variations:

#include <stdio.h>

int main(){

  int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};

  int j=0;   
  int *ptr = &n[0][0];

  for(j=0; j<9; j++)
  {
     printf("%d\t",*(ptr++)) ;
  }

  return 0; 
}

or

#include <stdio.h>

int main(){

  int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};

  int i, j;   
  int *ptr = &n[0][0];

  for(i=0; i<9; i+=3)
  {
    for(j=0; j<3; j++)
    {
     printf("%d\t",*(ptr+i+j)) ;
    }
  }

  return 0; 
}

And of course the old stand-by:

#include <stdio.h>

int main(){

  int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};

  int i, j;   

  for(i=0; i<3; i++)
  {
    for(j=0; j<3; j++)
    {
     printf("%d\t",n[i][j]);
    }
  }

  return 0; 
}

See if these different methods make sense for you. Ask questions if they don't.

EDIT a simple modification to your program shows better what the bug in your original code is:

#include <stdio.h>

int main(){
  int i, j;

  for(i=0; i<3; i++)
    for(j=0; j<9; j++)
    {
       printf("i = %d; j = %d; i + j = %d\n", i, j, i+j);
    }
   return 0;
}

Which prints out the values of i and j in the inner loop (where you were trying to print the array elements in turn). You get:

i = 0; j = 0; i + j = 0
i = 0; j = 1; i + j = 1
i = 0; j = 2; i + j = 2
i = 0; j = 3; i + j = 3
i = 0; j = 4; i + j = 4
i = 0; j = 5; i + j = 5
i = 0; j = 6; i + j = 6
i = 0; j = 7; i + j = 7
i = 0; j = 8; i + j = 8
i = 1; j = 0; i + j = 1
i = 1; j = 1; i + j = 2
i = 1; j = 2; i + j = 3
i = 1; j = 3; i + j = 4
i = 1; j = 4; i + j = 5
i = 1; j = 5; i + j = 6
i = 1; j = 6; i + j = 7
i = 1; j = 7; i + j = 8
i = 1; j = 8; i + j = 9
i = 2; j = 0; i + j = 2
i = 2; j = 1; i + j = 3
i = 2; j = 2; i + j = 4
i = 2; j = 3; i + j = 5
i = 2; j = 4; i + j = 6
i = 2; j = 5; i + j = 7
i = 2; j = 6; i + j = 8
i = 2; j = 7; i + j = 9
i = 2; j = 8; i + j = 10

As you can see, you get far too many elements printed, and you "fall off the edge" because when i + j > 8 you are accessing memory beyond that which was assigned. Which leads to Undefined Behavior. But it's usually a Bad Thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM