0

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.

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
frillybob
  • 600
  • 2
  • 12
  • 26
  • 3
    A `**pointer` is not the same as a 2D array, although it can be indexed in the same way. – Weather Vane Oct 31 '17 at 22:08
  • @WeatherVane How would I go about accessing elements of the pointer (that points to a regular 2d array)? Is that possible to do easily? Thanks – frillybob Oct 31 '17 at 22:09
  • @xing I think what you are trying to say is for a 1D array. I have tried something similar for a 2D array `int **pointer = c1;` `printf("%d\n", *(*(pointer + 0) + 0));` But this seg faults. – frillybob Oct 31 '17 at 22:26
  • As xing said, just `int (*pointer)[1000] = x ? a : c;` – Bob__ Oct 31 '17 at 22:30
  • Is this an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? If the arrays are defined globally, and not dynamic, what do you need pointers for? – Weather Vane Oct 31 '17 at 22:32
  • 2
    Take a look at [this](https://stackoverflow.com/questions/14808908/c-pointer-to-two-dimensional-array). – Vasilis G. Oct 31 '17 at 22:40
  • @xing Thank you that does do what I need it to. I thought you were originally saying that is how to access an element. The answer by user88665213 cleared it up. – frillybob Oct 31 '17 at 22:48

3 Answers3

2
//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;

}
0

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;
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • That's getting around the problem. I actually have quite a bit do with the array and don't want to duplicate all of the code. I just provided an example of what I was trying to do. – frillybob Oct 31 '17 at 22:18
  • Then post a more realistic question please. – Weather Vane Oct 31 '17 at 22:20
  • I am not sure what is not clear. i would like to do exactly as I posted in my question. I want a pointer to a 2D array. The pointer points to a global 2D array but it set in an if statement. – frillybob Oct 31 '17 at 22:25
0

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;
}
rIz
  • 1
  • 2