Dereferenceof ( Pointer to array of pointers to arrays of pointers to arrays of integers) to get the integeres at the end

huangapple go评论110阅读模式
英文:

Dereferenceof ( Pointer to array of pointers to arrays of pointers to arrays of integers) to get the integeres at the end

问题

我想声明指针ptr0,指向具有指向另一个包含整数元素的指针数组的指针的数组array1,这些整数元素是指针,指向另一个包含整数元素的指针数组的数组

注意:每个数组包含三个元素。

这是我的方法:-

//C99

#include <stdio.h>
#include <stdlib.h>
int main()
{

/* Level 3 */
int arrays_int_3_1_1[3] = {0,1,2};
int arrays_int_3_1_2[3] = {3,4,5};
int arrays_int_3_1_3[3] = {6,7,8};
    
int arrays_int_3_2_1[3] = {9,10,11};
int arrays_int_3_2_2[3] = {12,13,14};
int arrays_int_3_2_3[3] = {15,16,17};

int arrays_int_3_3_1[3] = {18,19,20};
int arrays_int_3_3_2[3] = {21,22,23};
int arrays_int_3_3_3[3] = {24,25,26};

/* Level 2 */
void *arrays_2_1[3] = {(void*)arrays_int_3_1_1,(void*)arrays_int_3_1_2,(void*)arrays_int_3_1_3};
void *arrays_2_2[3] = {(void*)arrays_int_3_2_1,(void*)arrays_int_3_2_2,(void*)arrays_int_3_2_3};
void *arrays_2_3[3] = {(void*)arrays_int_3_3_1,(void*)arrays_int_3_3_2,(void*)arrays_int_3_3_3}; 

/* Level 1*/
void **array1[3] = {arrays_2_1,arrays_2_2,arrays_2_3};

/* Level 0*/
void ***ptr0 = array1;

/*-----------------------------*/

for (int i = 0 ; i < 27 ; i++)
{
    // 问题在这里...
	printf("%d \n", ***ptr0);
    ptr0++;
}

}

问题是:如何解引用以访问每个元素?

英文:

I want to declare Pointer ptr0 that points to array1 of pointers to arrays of pointers that have elements of pointers that points to another arrays of pointers to arrays of integers that consists of integer elements.

Note: Each array consist of three elements.

here is my approach:-

//C99

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main()
{



/* Level 3 */
int arrays_int_3_1_1[3] = {0,1,2};
int arrays_int_3_1_2[3] = {3,4,5};
int arrays_int_3_1_3[3] = {6,7,8};
    
int arrays_int_3_2_1[3] = {9,10,11};
int arrays_int_3_2_2[3] = {12,13,14};
int arrays_int_3_2_3[3] = {15,16,17};

int arrays_int_3_3_1[3] = {18,19,20};
int arrays_int_3_3_2[3] = {21,22,23};
int arrays_int_3_3_3[3] = {24,25,26};


/* Level 2 */
void *arrays_2_1[3] = {(void*)arrays_int_3_1_1,(void*)arrays_int_3_1_2,(void*)arrays_int_3_1_3};
void *arrays_2_2[3] = {(void*)arrays_int_3_2_1,(void*)arrays_int_3_2_2,(void*)arrays_int_3_2_3};
void *arrays_2_3[3] = {(void*)arrays_int_3_3_1,(void*)arrays_int_3_3_2,(void*)arrays_int_3_3_3}; 

/* Level 1*/
void **array1[3] = {arrays_2_1,arrays_2_2,arrays_2_3};

/* Level 0*/
void ***ptr0 = array1;

/*-----------------------------*/

for (int i = 0 ; i &lt;27 ; i++)
{
    // Question here..
	printf(&quot;%d \n&quot;, &lt;??? How to do the dereference to access each element ???&gt;);
}

}

Quesiton is >> How to do the dereference to access each element

答案1

得分: 3

ptr0 指向 array1 的第一个元素(array1[0]),因此 ptr0 可以用作 array1 的别名。

array1 的每个元素指向 arrays_2_* 中一个的第一个元素,因此可以用作这些数组的别名。

arrays_2_* 中的每个元素都是指向 arrays_3_* 中一个的第一个元素的指针,转换为 void * 类型。将其转换回 int * 类型将得到相应的 arrays_int_3_* 的别名。

有多种方法可以执行解引用操作,但你可能正在寻找以下方式之一:

    int j = ((int *)ptr0[l][m])[n];
    //              \__a__/
    //              \___b____/
    //      \____________c____/
    //      \_____________d______/

    //  a: array1 的一个元素(一个 void **)
    //  b: arrays_2_* 中一个的一个元素(一个 void *)
    //  c: (b),转换为 int * 类型;这将指向 arrays_int_3_* 中的一个元素
    //  d: arrays_int_3_* 中一个的一个元素

如何将一个范围在 0 到 26 之间的单个整数 i 转换为三个整数 lmn,范围在 0 到 2 之间,留作练习。

英文:

ptr0 points to the first element of array1 (array1[0]), and ptr0 can therefore be used as an alias of array1.

The elements of array1 each point to the first element of one of the arrays_2_*, and can therefore be used as aliases for these.

Each element of each of the arrays_2_* is a pointer to the first element of one of the arrays_3_*, cast to type void *. Casting back to type int * will yield an alias for the corresponding arrays_int_3_*.

There is more than one way to perform the dereferencing, but you're probably looking for something along these lines:

    int j = ((int *)ptr0[l][m])[n];
    //              \__a__/
    //              \___b____/
    //      \____________c____/
    //      \_____________d______/

    //  a: one element of array1 (a void **)
    //  b: one element of one of the arrays_2_* (a void *)
    //  c: (b), converted to type int *; this points into one of the arrays_int_3_*
    //  d: one element of one of the arrays_int_3_*

How to convert a single integer i, in the range 0 ... 26, to a triple l, m, n, each in the range 0 ... 2, is left as an exercise.

答案2

得分: 2

不确定为什么您将它们强制转换为 void*,但我将它们重新转换为打印它们。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    /* Level 3 */
    int arrays_int_3_1_1[3] = {0, 1, 2};
    int arrays_int_3_1_2[3] = {3, 4, 5};
    int arrays_int_3_1_3[3] = {6, 7, 8};
    
    int arrays_int_3_2_1[3] = {9, 10, 11};
    int arrays_int_3_2_2[3] = {12, 13, 14};
    int arrays_int_3_2_3[3] = {15, 16, 17};

    int arrays_int_3_3_1[3] = {18, 19, 20};
    int arrays_int_3_3_2[3] = {21, 22, 23};
    int arrays_int_3_3_3[3] = {24, 25, 26};

    /* Level 2 */
    void *arrays_2_1[3] = {(void *)arrays_int_3_1_1, (void *)arrays_int_3_1_2, (void *)arrays_int_3_1_3};
    void *arrays_2_2[3] = {(void *)arrays_int_3_2_1, (void *)arrays_int_3_2_2, (void *)arrays_int_3_2_3};
    void *arrays_2_3[3] = {(void *)arrays_int_3_3_1, (void *)arrays_int_3_3_2, (void *)arrays_int_3_3_3};

    /* Level 1 */
    void **array1[3] = {arrays_2_1, arrays_2_2, arrays_2_3};

    /* Level 0 */
    void ***ptr0 = array1;

    /*-----------------------------*/

    for (int i = 0; i < 27; i++)
    {
        printf("%d \n", ((int *)(ptr0[i / 9])[(i / 3) % 3])[i % 3]);
    }
}
英文:

Not sure why you are casting them to void* but I cast them back to print them.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main()
{



/* Level 3 */
int arrays_int_3_1_1[3] = {0,1,2};
int arrays_int_3_1_2[3] = {3,4,5};
int arrays_int_3_1_3[3] = {6,7,8};
    
int arrays_int_3_2_1[3] = {9,10,11};
int arrays_int_3_2_2[3] = {12,13,14};
int arrays_int_3_2_3[3] = {15,16,17};

int arrays_int_3_3_1[3] = {18,19,20};
int arrays_int_3_3_2[3] = {21,22,23};
int arrays_int_3_3_3[3] = {24,25,26};


/* Level 2 */
void *arrays_2_1[3] = {(void*)arrays_int_3_1_1,(void*)arrays_int_3_1_2,(void*)arrays_int_3_1_3};
void *arrays_2_2[3] = {(void*)arrays_int_3_2_1,(void*)arrays_int_3_2_2,(void*)arrays_int_3_2_3};
void *arrays_2_3[3] = {(void*)arrays_int_3_3_1,(void*)arrays_int_3_3_2,(void*)arrays_int_3_3_3}; 

/* Level 1*/
void **array1[3] = {arrays_2_1,arrays_2_2,arrays_2_3};

/* Level 0*/
void ***ptr0 = array1;

/*-----------------------------*/

for (int i = 0 ; i &lt; 27 ; i++)
{
    printf(&quot;%d \n&quot;, ((int*)(ptr0[i/9])[(i/3)%3])[i%3]);
}

}

huangapple
  • 本文由 发表于 2023年3月7日 04:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定