Pointer to a 3d arrayWhat are the differences between a pointer variable and a reference variable in C++?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?With arrays, why is it the case that a[5] == 5[a]?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why should I use a pointer rather than the object itself?

How to prevent pickpocketing in busy bars?

Assembly of PCBs containing a mix of SMT and thru-hole parts?

"until mine is on tight" is a idiom?

Top off gas with old oil, is that bad?

Why is Pelosi so opposed to impeaching Trump?

Which altitudes are safest for VFR?

Can an energy drink or chocolate before an exam be useful ? What sort of other edible goods be helpful?

How can the dynamic linker/loader itself be dynamically linked as reported by `file`?

How to stop the death waves in my city?

Are the coefficients of certain product of Rogers-Ramanujan Continued Fraction non-negative?

Does the app TikTok violate trademark?

When did Unix stop storing passwords in clear text?

What is Weapon Handling?

Garage door sticks on a bolt

Sci-fi movie with one survivor and an organism(?) recreating his memories

What would influence an alien race to map their planet in a way other than the traditional map of the Earth

Why is STARTTLS still used?

What can Thomas Cook customers who have not yet departed do now it has stopped operating?

Dynamic DataSource for Droplist in Content Editor

Maximize assigned tasks to each worker

Smallest PRIME containing the first 11 primes as sub-strings

A word that refers to saying something in an attempt to anger or embarrass someone into doing something that they don’t want to do?

How do I introduce dark themes?

What happens to a net with the Returning Weapon artificer infusion after it hits?



Pointer to a 3d array


What are the differences between a pointer variable and a reference variable in C++?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?With arrays, why is it the case that a[5] == 5[a]?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why should I use a pointer rather than the object itself?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















I would like to declare a pointer to a 3d array in a manner that I could index it with one dimension less than original array, example: ptr[i][j]. I'm using some structure in which I would like this pointer to be stored so I could access it later.



I've done this with the 2d arrays but I declared a pointer to an array of pointers to 2d arrays:



typedef const unsigned char* ptrType;
const ptrType ptrArray[] = ...;


And this is what I'm trying for the 3d array:



typedef const unsigned char** ptrType;

typedef struct structP

ptrType arrayPtr;
;


and in the main I'm doing something like this:



struct structP test;
test.arrayPtr = *myThreeDArray;


when trying to access elements via pointer this is the only thing that compiler let me do:



&(test.arrayPtr[i][j]);


Also myThreeDArray is defined like this:



const unsigned char myThreeDArray[2][23][25] = ... ;


Doing it in a way that is described gives me unspected results at the output. Some garbage values.



Any ideas how to do this in a proper way?










share|improve this question





















  • 2





    Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

    – πάντα ῥεῖ
    Mar 28 at 19:34

















-2















I would like to declare a pointer to a 3d array in a manner that I could index it with one dimension less than original array, example: ptr[i][j]. I'm using some structure in which I would like this pointer to be stored so I could access it later.



I've done this with the 2d arrays but I declared a pointer to an array of pointers to 2d arrays:



typedef const unsigned char* ptrType;
const ptrType ptrArray[] = ...;


And this is what I'm trying for the 3d array:



typedef const unsigned char** ptrType;

typedef struct structP

ptrType arrayPtr;
;


and in the main I'm doing something like this:



struct structP test;
test.arrayPtr = *myThreeDArray;


when trying to access elements via pointer this is the only thing that compiler let me do:



&(test.arrayPtr[i][j]);


Also myThreeDArray is defined like this:



const unsigned char myThreeDArray[2][23][25] = ... ;


Doing it in a way that is described gives me unspected results at the output. Some garbage values.



Any ideas how to do this in a proper way?










share|improve this question





















  • 2





    Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

    – πάντα ῥεῖ
    Mar 28 at 19:34













-2












-2








-2








I would like to declare a pointer to a 3d array in a manner that I could index it with one dimension less than original array, example: ptr[i][j]. I'm using some structure in which I would like this pointer to be stored so I could access it later.



I've done this with the 2d arrays but I declared a pointer to an array of pointers to 2d arrays:



typedef const unsigned char* ptrType;
const ptrType ptrArray[] = ...;


And this is what I'm trying for the 3d array:



typedef const unsigned char** ptrType;

typedef struct structP

ptrType arrayPtr;
;


and in the main I'm doing something like this:



struct structP test;
test.arrayPtr = *myThreeDArray;


when trying to access elements via pointer this is the only thing that compiler let me do:



&(test.arrayPtr[i][j]);


Also myThreeDArray is defined like this:



const unsigned char myThreeDArray[2][23][25] = ... ;


Doing it in a way that is described gives me unspected results at the output. Some garbage values.



Any ideas how to do this in a proper way?










share|improve this question
















I would like to declare a pointer to a 3d array in a manner that I could index it with one dimension less than original array, example: ptr[i][j]. I'm using some structure in which I would like this pointer to be stored so I could access it later.



I've done this with the 2d arrays but I declared a pointer to an array of pointers to 2d arrays:



typedef const unsigned char* ptrType;
const ptrType ptrArray[] = ...;


And this is what I'm trying for the 3d array:



typedef const unsigned char** ptrType;

typedef struct structP

ptrType arrayPtr;
;


and in the main I'm doing something like this:



struct structP test;
test.arrayPtr = *myThreeDArray;


when trying to access elements via pointer this is the only thing that compiler let me do:



&(test.arrayPtr[i][j]);


Also myThreeDArray is defined like this:



const unsigned char myThreeDArray[2][23][25] = ... ;


Doing it in a way that is described gives me unspected results at the output. Some garbage values.



Any ideas how to do this in a proper way?







c arrays pointers multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 19:39







minimalistic

















asked Mar 28 at 19:30









minimalisticminimalistic

83 bronze badges




83 bronze badges










  • 2





    Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

    – πάντα ῥεῖ
    Mar 28 at 19:34












  • 2





    Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

    – πάντα ῥεῖ
    Mar 28 at 19:34







2




2





Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

– πάντα ῥεῖ
Mar 28 at 19:34





Please provide a minimal reproducible example that reproduces your problem as required here. Also use the language tags appropriately, c language and c++ aren't the same.

– πάντα ῥεῖ
Mar 28 at 19:34












1 Answer
1






active

oldest

votes


















2
















What you seem to want is a pointer to a 2D array



For integers this could be like:



#include <stdio.h>

void print_3d(int (*p3d)[3][4])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
for (int k=0; k<4; ++k)
printf("%d ", p3d[i][j][k]);


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

int arr3d[2][3][4] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
;

int (*p_to_arr3d)[3][4] = arr3d; // Get a pointer to int[3][4]
print_3d(p_to_arr3d); // Use the pointer



Output:



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 


In case you are dealing with strings, it could be like:



#include <stdio.h>

void print_3d(char (*p3d)[3][20])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
printf("%s ", p3d[i][j]);


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

char arr3d[2][3][20] =
"this", "is", "just",
"a", "little", "test"
;

char (*p_to_arr3d)[3][20] = arr3d; // Get a pointer to char[3][20]
print_3d(p_to_arr3d);



Output:



this is just a little test


Using the same syntax as above you can store the pointer in a struct:



struct myData

char (*p_to_char_arr)[3][20];
int (*p_to_int_arr)[3][4];
;





share|improve this answer



























  • This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

    – minimalistic
    Mar 28 at 20:09












  • @minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

    – 4386427
    Mar 28 at 20:12














Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405529%2fpointer-to-a-3d-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















What you seem to want is a pointer to a 2D array



For integers this could be like:



#include <stdio.h>

void print_3d(int (*p3d)[3][4])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
for (int k=0; k<4; ++k)
printf("%d ", p3d[i][j][k]);


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

int arr3d[2][3][4] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
;

int (*p_to_arr3d)[3][4] = arr3d; // Get a pointer to int[3][4]
print_3d(p_to_arr3d); // Use the pointer



Output:



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 


In case you are dealing with strings, it could be like:



#include <stdio.h>

void print_3d(char (*p3d)[3][20])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
printf("%s ", p3d[i][j]);


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

char arr3d[2][3][20] =
"this", "is", "just",
"a", "little", "test"
;

char (*p_to_arr3d)[3][20] = arr3d; // Get a pointer to char[3][20]
print_3d(p_to_arr3d);



Output:



this is just a little test


Using the same syntax as above you can store the pointer in a struct:



struct myData

char (*p_to_char_arr)[3][20];
int (*p_to_int_arr)[3][4];
;





share|improve this answer



























  • This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

    – minimalistic
    Mar 28 at 20:09












  • @minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

    – 4386427
    Mar 28 at 20:12
















2
















What you seem to want is a pointer to a 2D array



For integers this could be like:



#include <stdio.h>

void print_3d(int (*p3d)[3][4])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
for (int k=0; k<4; ++k)
printf("%d ", p3d[i][j][k]);


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

int arr3d[2][3][4] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
;

int (*p_to_arr3d)[3][4] = arr3d; // Get a pointer to int[3][4]
print_3d(p_to_arr3d); // Use the pointer



Output:



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 


In case you are dealing with strings, it could be like:



#include <stdio.h>

void print_3d(char (*p3d)[3][20])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
printf("%s ", p3d[i][j]);


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

char arr3d[2][3][20] =
"this", "is", "just",
"a", "little", "test"
;

char (*p_to_arr3d)[3][20] = arr3d; // Get a pointer to char[3][20]
print_3d(p_to_arr3d);



Output:



this is just a little test


Using the same syntax as above you can store the pointer in a struct:



struct myData

char (*p_to_char_arr)[3][20];
int (*p_to_int_arr)[3][4];
;





share|improve this answer



























  • This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

    – minimalistic
    Mar 28 at 20:09












  • @minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

    – 4386427
    Mar 28 at 20:12














2














2










2









What you seem to want is a pointer to a 2D array



For integers this could be like:



#include <stdio.h>

void print_3d(int (*p3d)[3][4])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
for (int k=0; k<4; ++k)
printf("%d ", p3d[i][j][k]);


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

int arr3d[2][3][4] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
;

int (*p_to_arr3d)[3][4] = arr3d; // Get a pointer to int[3][4]
print_3d(p_to_arr3d); // Use the pointer



Output:



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 


In case you are dealing with strings, it could be like:



#include <stdio.h>

void print_3d(char (*p3d)[3][20])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
printf("%s ", p3d[i][j]);


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

char arr3d[2][3][20] =
"this", "is", "just",
"a", "little", "test"
;

char (*p_to_arr3d)[3][20] = arr3d; // Get a pointer to char[3][20]
print_3d(p_to_arr3d);



Output:



this is just a little test


Using the same syntax as above you can store the pointer in a struct:



struct myData

char (*p_to_char_arr)[3][20];
int (*p_to_int_arr)[3][4];
;





share|improve this answer















What you seem to want is a pointer to a 2D array



For integers this could be like:



#include <stdio.h>

void print_3d(int (*p3d)[3][4])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
for (int k=0; k<4; ++k)
printf("%d ", p3d[i][j][k]);


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

int arr3d[2][3][4] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
;

int (*p_to_arr3d)[3][4] = arr3d; // Get a pointer to int[3][4]
print_3d(p_to_arr3d); // Use the pointer



Output:



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 


In case you are dealing with strings, it could be like:



#include <stdio.h>

void print_3d(char (*p3d)[3][20])

for (int i=0; i<2; ++i)
for (int j=0; j<3; ++j)
printf("%s ", p3d[i][j]);


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

char arr3d[2][3][20] =
"this", "is", "just",
"a", "little", "test"
;

char (*p_to_arr3d)[3][20] = arr3d; // Get a pointer to char[3][20]
print_3d(p_to_arr3d);



Output:



this is just a little test


Using the same syntax as above you can store the pointer in a struct:



struct myData

char (*p_to_char_arr)[3][20];
int (*p_to_int_arr)[3][4];
;






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 20:01

























answered Mar 28 at 19:53









43864274386427

24.2k3 gold badges20 silver badges47 bronze badges




24.2k3 gold badges20 silver badges47 bronze badges















  • This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

    – minimalistic
    Mar 28 at 20:09












  • @minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

    – 4386427
    Mar 28 at 20:12


















  • This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

    – minimalistic
    Mar 28 at 20:09












  • @minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

    – 4386427
    Mar 28 at 20:12

















This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

– minimalistic
Mar 28 at 20:09






This is just what I was looking for. Can you please explain why is this the right way and what I was doing wrong? I understand what I'm doing with 2d arrays. But when I increase the dimensions I get lost.

– minimalistic
Mar 28 at 20:09














@minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

– 4386427
Mar 28 at 20:12






@minimalistic Why is it right? Well, if you have int arr[N] the variable arr decays into a pointer to int (i.e. int *arr). Likewise, if you have int arr[M][N] the variable arr decays into a pointer to int[N] (i.e. int (*arr)[N]) and again if you have int arr[K][M][N]` the variable arr decays into a pointer to int[M][N] (i.e. int (*arr)[M][N]). . As you can see it's all about "removing" the left most dimension.

– 4386427
Mar 28 at 20:12





















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405529%2fpointer-to-a-3d-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현