简体   繁体   中英

Getting a pointer to a 2d Array

I need to get a pointer to a 2D array. The sizes ARE known at compile time if that helps. I need to perform an action a certain array based on the incoming value of a variable.

//Global arrays
// int c[6000][1000];
// int a[6000][1000];

void fun(int x){

    //Setup a pointer here
    //Possible solution: int (*pointer)[6000][1000];
    int **pointer;
    if (x == 0){
         pointer = c;
    }
    else{
         pointer = a;
    }
    //Modify pointer here and have changes reflect back to the array it was based off of
    pointer[0][17] = 42;
}

I have looked at close to a dozen different stack overflow articles on how to do this but I cannot find a way to just a get a simple pointer to a 2D array.

//Global arrays
int c[6000][1000];
int a[6000][1000];

void fun(int x) {
 int  (* ptr)[1000];
  if (x == 0) {
    ptr = c;
   } else {
    ptr = a;
   }
   ptr[0][17] = 42;

}

Like this

//Global arrays
int c[6000][1000];
int a[6000][1000];

void fun(int x) {
    if (x == 0) {
        c[0][17] = 42;
    } else {
        a[0][17] = 42;
    }
}

Accessing a 2D array using Pointers: 2D array is an array of 1D arrays which implies each row of a 2D array is a 1D array. So, think about two of your global arrays, int c[6000][1000] and int a[6000][1000]. We can say c[0] is the address of row 0 of the first global 2D array. Similarly a[6000] is the address of row 6000 of the second global 2D array. Now you want to find the c[0][17] and point that using a pointer which is basically, c[0][17] = *(c[0] + 17). Also you can write for any other array elements, c[0] = *c and in general each element as, c[i][j] = *(c[i] + j) = ( (c+i) + j).

So, if c is a 2D array of integer type then we can think that c is a pointer to a pointer to an integer which can be interpreted as int **c. Dereferencing *c gives you the address of row 0 or c[0] which is a pointer to an integer and again dereferencing c[0] gives you the first element of the 2D array, c[0][0] which is an integer. You can test your code by accessing each element using pointer for a better understanding:

#include<stdio.h>
#include<stdlib.h>

//Global arrays
int c[6000][1000];
int a[6000][1000];

void fun(int x){
    //Setup a pointer here
    //Possible solution: int (*pointer)[6000][1000];
    int (*pointer)[1000];
    if (x == 0){
         pointer = c;
    }
    else{
         pointer = a;
    }
    //Modify pointer here and have changes reflect back to the array it was based off of
    pointer[0][17] = 42;
}

int main(){
    int num;
    scanf("%d", &num);
    fun(num);
    if(num == 0){
        printf("When num is %d, c[0][17] = %d\n", num, *(*(c) + 17));
    }
    else{
        printf("When num is %d, a[0][17] = %d\n", num, *(a[0] + 17));
    }
    return 0;
}

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