0

Let's say I'm creating a multidimensional array as follows.

const int array_temp[3][2][1] = {
    [0] = {{0}, {1}},
    [1] = {{2}, {3}},
    [2] = {{4}, {5}},
};

This array represents 3 tables of 2 entries in each table.

How would I return a pointer to the start of one of the 3 tables? Would it be as follows?

return (void *)&array_temp[idx][0][0];
KAM
  • 115
  • 1
  • 3
  • 5
  • 1
    Which language are you working in? – Richard Jul 13 '17 at 18:44
  • @Richard Pointer are in c/c++ ? – Pushan Gupta Jul 13 '17 at 18:46
  • Have you considered saving on painkillers and just returning a `std::vector`? – Jesper Juhl Jul 13 '17 at 18:50
  • This seems to be C-specific syntax (not C++) – anatolyg Jul 13 '17 at 18:52
  • A [related question](https://stackoverflow.com/q/8617889/509868) which I answered – anatolyg Jul 13 '17 at 18:57
  • 2
    If you're declaring the array inside a function, you can't return a pointer to it, since the array is destroyed when the function ends. – Barmar Jul 13 '17 at 18:58
  • Sorry... this is in C. and I meant for the code to be: return (void *)&array_temp[idx][0][0]; – KAM Jul 13 '17 at 18:58
  • `array_temp[idx]` for a pointer to arrays of 1 `int`, or `&(array_temp[idx])` for a pointer to a 2x1 `int` array (same address either way). – Dmitri Jul 13 '17 at 18:58
  • I think that designated initializers are not in C++14 (nor in C++17), but some compilers may allow them regardless. That suggests your code is C rather than C++. But if you are using C++, you have superior tools at your service. This really is a place where the answer to the C question is radically different from the sensible answer for the C++ question. [New features in C++17](https://stackoverflow.com/questions/38060436/) indicates not in C++17 either; there's a Reddit [thread](https://www.reddit.com/r/cpp/comments/4ekova/iso_c_standard_future_proposals_rfc_uniform/) affirming that. – Jonathan Leffler Jul 13 '17 at 18:58
  • What type are you planning to return from the function? How are you going to use the function result in the calling code? That's probably more interesting than the notation used to return a suitable pointer. Note that `array_temp[idx][0][0]` is an `int`, so it isn't a good value to return as a pointer. (Oh — you [commented](https://stackoverflow.com/questions/45088666/returning-a-pointer-to-multidimensional-array#comment77150366_45088666) instead of editing! I've edited for you.) Still, returning an `int *` via `void *` isn't immediately obviously correct. – Jonathan Leffler Jul 13 '17 at 19:06
  • How to understand this: "*pointer to the start of one of the 3 tables*"? 1) get a pointer to one of the three tables? Or 2) get a pointer to the 1st element of one of the three tables? – alk Jul 15 '17 at 13:26

3 Answers3

0

In a 3D case, derefernce the pointer name once leads us to the Zeroth row. This means that adding idx to the dereferenced name would lead us to the idx'th row(Where idx>=0)

return ((*array_temp)+idx);

But be sure that you don't declare the array inside the function. As its duration is till the function ends

Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
-1

Try return array_temp[idx]. the return array_temp[idx][0][0] will return a specific content not the (container) array itself.

mkr
  • 11
  • 3
  • As a recommendation, try to avoid low-level pointer manipulations, as they are error-prone and hurt the quality of your code. Wrap them in higher-level container/objects or use the STL (if possible). – mkr Jul 13 '17 at 18:57
  • Would i need to do: return array_temp[idx] or return &array_temp[idx] .... i know an array name is a pointer to the first element in an array but in the case of a multidimensional array is the indexed entry also a pointer? – KAM Jul 14 '17 at 17:12
-1

This example is self explanatory I think

#include <stdio.h> 

char arr[4][5][6];



int main(int argc, char *argv[]) {

    printf("%d\n\r", sizeof *(arr));
    printf("%d\n\r", sizeof *(arr[0]));
    printf("%d\n\r", sizeof *(arr[0][0]));
    printf("%d\n\r", sizeof *(&arr));
    printf("%d\n\r", sizeof *(&arr[0]));
    printf("%d\n\r", sizeof *(&arr[0][0]));

}

And the result:

30
6
1
120
30
6

Nothing to add I think

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I'm not sure what this answer purports to show. I'm not clear how it relates to the question. – Jonathan Leffler Jul 30 '17 at 22:26
  • Yeah. That's difficult. Let me explain if it too difficult for you. 1. Pointer to the dwo dimensional array with 30 elements. As the advanced math shows it will be first [5][6] array. are +1 logically the second. 2nd pointer to 6 elements array - guess which one. 3rd result I hope you can answer yourself. Then. 4th, 5th and 6th result - if the puzzle is too complex let me know. Using ones brain it is easy to understand how to return pointer to the particular n dimensional subarray. – 0___________ Jul 31 '17 at 00:39