当前位置: 动力学知识库 > 问答 > 编程问答 >

C : Pointer for two dimension array

问题描述:

I can use pointer for one-dimension array for function, but I don't know use the same technique for two-dimension array.

Here is my example code :

 int main(){

int b[10]; //initialize b

Print(b);

int a[10][10]; //initialize a

Print2(a);

}

void Print(int*b){

for(int i=0; i<10; i++)

printf("%d\n",*(b+i));

}

// I want to use same technique here

// but i have met error

void Print2(int*a){

for(int i=0; i<10; i++)

for(int j=0; j<10; j++)

printf("%d\n",*(*(a+i)+j)); // error at this line

}

Please help me how to use same technique of one-dimension array for two-dimension array.

Thanks :)

网友答案:

destroy the inner * and multiply i with 10 then add j

 printf("%d\n",*((a+i*10)+j));

because going "downwards" in a 2d matrix, you go through dimension-1(10 in this example) elements.

for 3d:

 printf("%d\n",*(a+i*100+j*10+k));

for N-dimensions:

  *(a+i*(size of i-dimension)+j*(size of j dim)+......+last index)
网友答案:

I've found this paste extremely useful, called "array decay". Check the comments above each example line for the type.

http://ideone.com/1PyNC

网友答案:

The thing is, an array decays to a pointer, but a two-dimendional array decays to a pointer to an array of specific size.

#include "stdio.h"

void print(int *arr);
void print2D(int (*arr)[]);

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

    int data2D[5][5];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            data2D[i][j] = (i + 1) * 10 + (j + 1);
        }
    }
    print2D(data2D);
}

void print(int *arr) {  
    for (int i = 0; i < 5; i++) {
        printf("%d\n", arr[i]);
    }
}

void print2D(int (*arr)[5]) {  
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%d\n", arr[i][j]);
        }
    }
}

Here, as you can see, I declared array in the two-dimensional function as a pointer to arrays of 5 elements. Now when I increment i, the compiler knows that I want to skip five elements. You can run this example at Ideone.

You can do it manually, of course. In this case you will have to calculate the address yourself. In this case, the function will look like this:

void print2D(int *arr) {  
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%d\n", *(arr + (i * 5) + j));
        }
    }
}

You will have to call it this way:

print2D(data2D[0]);

The second example on Ideone.

网友答案:

b is an array of ints, so its first element is an int. When b decays to a pointer to its first element, the result is therefore an int* (and that's what your Print function accepts).

a is an array of arrays, so its first element is an array. When a decays to a pointer to its first element, the result is therefore a pointer-to-array-of-10-ints:

void Print2(int (*a)[10] ){    // pointer-to-array type
    for(int i=0; i<10; i++)
        for(int j=0; j<10; j++)
            printf("%d\n",a[i][j]);
}

Multi-dimensional arrays only get you so far in C, because when you pass them around only the first dimension is variable. The remaining dimensions need to be known at compile-time because they're part of the pointer type. If you need something that's variable in multiple dimensions, you can "flatten" it as in tuğrul büyükışık's answer and use a 1-dimensional array.

网友答案:
        #include <stdio.h>

        void Print(int*b)
        {
        int i;
           for(i=0; i<10; i++)
              printf("%d\n",*(b+i));
        }


        void Print2(int*a)
        {
           int i,j;
           for( i=0; i<10; i++)
           {
              printf("\n");
              for(j=0; j<10; j++)
                  printf("%d\t",*(a+(i*10)+j));  // Modification done here.
           }
        }

        int main()
        {
          int b[10]; //initialize b
          Print(b);
          int a[10][10] = {}; //initialize a 0
          Print2((int*)a);
          return 0;
        }

I have modified the Print2().

Add the (Size of 1D Array * Which 1D array to be accessed(i)) + particular element in 1D Array(j) to Base Address of Array.

分享给朋友:
您可能感兴趣的文章:
随机阅读: