0

Assume there is an N-dimentional array:

type list[S1][S2]...[SN]

How to create a pointer to this?

One could create another N-1-dimentional array of pointers by getting addresses of the lowest-level arrays, then getting addresses of the newly created arrays of pointers and so on, but this sounds like a complete mess. Typedef may come in handy, but that is not a fast way as well. What options are there to create a pointer to the whole array or to any level of this array?

Kaiyaha
  • 448
  • 3
  • 9
  • "... a pointer to the whole array or to any level of this array?" For each item in the array? With a `for`-loop you can reach this simple... – SwissCodeMen Oct 18 '20 at 21:37
  • @SwissCodeMen the problem is that except for the lowest-level arrays, each item in each array is a multidimentional array on its own – Kaiyaha Oct 18 '20 at 21:39
  • @Kaiyaha why is it a problem? – 0___________ Oct 18 '20 at 21:41
  • 4
    *"How to create a pointer to this?"* - `type (*ptr)[S2][S3]....[SN] = list;` – WhozCraig Oct 18 '20 at 21:42
  • ***`typedef may come in handy, but that is not a fast way as well`*** typdef does not add any overhead so this statement is false by default – 0___________ Oct 18 '20 at 21:42
  • 1
    @WhozCraig it is not pointer to this. It is pointer to `type [S2]...[SN]` – 0___________ Oct 18 '20 at 21:43
  • @P__J__ By a _fast way_ I imply typing code, not compiling and executing it – Kaiyaha Oct 18 '20 at 21:43
  • @P__J__ I have no idea what you're talking about. The OP asked how to create a proper pointer to `list`, and that's what it does. If the OP wants something else perhaps they should clarify what the real *problem* is. – WhozCraig Oct 18 '20 at 21:44
  • @Kaiyaha if you're declaring a by-address formal declaration, then yes, you're correct. It would be `type (*ptr)[S1][S2]...[SN] = &list;` . Note the addressof operator applied to `list`. Otherwise it is as I showed prior. I sincerely doubt you want the latter. – WhozCraig Oct 18 '20 at 21:49
  • @WhozCraig That works, but I didn't get it why – Kaiyaha Oct 18 '20 at 21:49
  • @P__J__ Really? Then why deal with functions and classes? Let's just type more! – Kaiyaha Oct 18 '20 at 21:50
  • actually C++ requires far more typing. Classes are not to "type" less – 0___________ Oct 18 '20 at 21:51
  • @WhozCraig Could you please explain your proposal as an answer? This is the solution I've been looking for – Kaiyaha Oct 18 '20 at 21:54
  • @P__J__ Programmers like typing more, but somewhy they defined `integer` as `int` :) – Kaiyaha Oct 18 '20 at 21:57
  • @Kaiyaha I am speechless. – 0___________ Oct 18 '20 at 21:58
  • @Kaiyaha it is explained in the dupe. You need to learn the pointers – 0___________ Oct 18 '20 at 21:59
  • btw real programmers do not save on typing. They save on debugging using names which mean something (`static void configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)`). – 0___________ Oct 18 '20 at 22:07
  • @P__J__ This all depends on a particular situation. And even here somewhy you name your struct pointer as `sd` – Kaiyaha Oct 18 '20 at 22:22
  • @P__J__ Definitely the more senseful a name is the easier a programme is to debug. Still I doubt you would name your function as you did. Long constructions affect readability too – Kaiyaha Oct 18 '20 at 22:24

1 Answers1

1

I would use typedef myself. Here you have an example: array of arrays;

#include <stdio.h>

typedef double arrd_t[2][3][4][5][6][7];
typedef arrd_t arrd_t_t[4][5][6][8][9][10];

arrd_t_t array;
arrd_t_t *ptr = &array;


double ret(arrd_t_t *ptr, size_t x,size_t y, size_t z, size_t a,size_t b, size_t c, size_t x1,size_t y1, size_t z1, size_t a1,size_t b1, size_t c1)
{
    return (*ptr)[x][y][z][a][b][c][x1][y1][z1][a1][b1][c1];
}

https://godbolt.org/z/G4oxsc

0___________
  • 60,014
  • 4
  • 34
  • 74