3D array question

Hi,

I need some help with 3d arrays. I read online that if you want to assign a pointer to a 3d array u'd need to put *** in front, but i get the following error.

error: cannot convert ‘int [1][3][2]’ to ‘int*’ in assignment

i also tried putting *x1 instead of ***x1 but similar issue

I have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

static int ***x1;
static int x,y;

int main() {
    int n = 2;
    int m = 3;
    int x2 [1][3][2];
    x1 = x2;
    return 0;
}


Any help is really appreciated!!
An array is not a pointer.

x1 needs to be declared as follows:

static int (*x1)[3][2];
Hi,

Thanks for your reply! How would i do it if the array sizes were variables set within main? The problem with that is that n and m wouldn't be defined outside of main.

i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

static int ***x1;
static int x,y;

int main() {
    int n = 2;
    int m = 3;
    int x2 [n][n][m];
    x1 = x2;
    return 0;
}


thanks again
In that case, you will have to cast x2 to an int***. The problem with this is that you lose the ability to determine the array's size. But anyway:

x1 = (int***)x2;
sweet! yes that worked. Thank you very much.
Sorry one more question.

So now when I try to read values from this x1 it gives seg fault.

ie if i do:

y = x1[9][8][7];

am i doing something else wrong?
Yes, the 3-D array you declared is not that large; for that to work, x2 must be at least

int x2[10][9][8];
Last edited on
Topic archived. No new replies allowed.