2

I know how can I use pointers in C with the 1-D array. like the following: but what if we have 2-D arrays? how can I address them by pointers? Thank you.

#include <stdio.h>

int main() {
  int dar[4] = {1,2,3,4};

  int *sar = NULL;
  sar = dar;
  for (int i = 0; i < 4; i++) {
    printf("%d ", *(sar + i));
    }
}
Dariyoush
  • 500
  • 4
  • 16

3 Answers3

1

This may also help.

 #include <stdio.h>

int main() 
{   
    int dar[2][3] = {1,2,3,
                    4,5,6}; 
    int index=0;
    for (int line = 0; line < 2; line++) 
    {
        for (int col=0; col<3;col++)
        {               
            printf("%d ", *(dar[0]+index));
            index=index+1;
        }
        printf("\n");
    }   
    return (0);

}

manoliar
  • 172
  • 1
  • 8
  • It works, but is bad. You are aliasing a 2-D array 2x3 to a 1-D array of size 6. According to this [other question of mine](https://stackoverflow.com/q/42951932/3545273), this is unspecified by the standard and hence formal Undefined Behaviour. – Serge Ballesta Oct 10 '18 at 07:39
  • @Serge Ballesta Aliasing a 2-D array to a 1-D array was exactly my purpose. I studied the link that you provided but I did not come to the same understanding as you. I am not an expert in C but I cannot agree with you. The main reason is that a static array is in a contiguously allocated block of memory. So to access its elements is a matter of simple pointer arithmetic. Please excuse my bad English. – manoliar Oct 11 '18 at 14:13
0

Thanks to @Qubit I solved the problem. I post the answer for future reference.

#include <stdio.h>

int main() {
  int dar[2][3] = {{1,2,3},
                   {4,5,6}};

  int *sar = NULL;
  int *bar = NULL;
  sar = dar[0];
  bar = dar[1];

  for (int i = 0; i < 3; i++) {
    printf("%d ", *(sar+i));
   }
  printf("\n");
  for (int j = 0; j < 3; j++) {
    printf("%d ",*(bar+j));
   }

   printf("\n");
}
Dariyoush
  • 500
  • 4
  • 16
  • That really isn't what I had in mind. This might work in this case, because it is small, but will scale miserably. I strongly suggest you read up on how arrays are stored. – Qubit Oct 09 '18 at 15:40
  • thanks @Qubit sure, now I will head up to find some good articles. :) – Dariyoush Oct 09 '18 at 15:47
  • I have posted an improved version of your code below. Feel free to re-use it in your own answer if you like, and accept it (your one). If you ping me in a comment after doing that, I will probably upvote yours and delete mine. – Serge Ballesta Oct 09 '18 at 16:09
0

A 2-D array is... a 1-D array of 1-D arrays, and you can use the common pointer arithmetics on both.

That means that you can simply do:

#include <stdio.h>

int main() {
  int dar[2][3] = {{1,2,3},
                   {4,5,6}};


  for (int i = 0; i < 2; i++) {
    int *bar = *(dar + i);           // pointer to the row
    for (int j = 0; j < 3; j++) {
      printf("%d ",*(bar+j));        // access the values
     }
     printf("\n");
  }
  printf("\n");
}

It works, because in dar + i, dar decays to a pointer to its first row, so *(dar + 1) (which is *by definition dar[i]) represents the i-th row and in turn decays to a pointer to the first element of that row.


Disclaimer: this is just an addition to JJcopl's answer, but too rich to fit in a comment...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252