创建三维指针 [英] Create a d-dimensional pointer
问题描述
我们用来用符号*
表示一个指针.重复该过程,我们获得一个双"指针**
,一个三重"指针***
,更一般地,一个"d维"指针(对于每个d个正自然数).
We use to indicate a pointer with the symbol *
. Iterating the procedure, we obtain a "double" pointer **
, a "triple" pointer ***
and, more generally, a "d-dimensional" pointer (for each d positive natural number).
问题:给定这样的d作为输入,将S定义为d维指针.
Problem: given such a d as an input, define S to be a d-dimensional pointer.
由于我是几天前才开始研究动态结构的,因此我对此问题有点麻烦. 有人有建议/提示吗?
Because I started studying dynamic structures only some days ago, I am unfortunately a bit in trouble with this problem. Does anyone have a suggestion/hint?
预先感谢您,对于可能太基本的问题,我深表歉意.
Thank you in advance, and my apologies for my probably too basic question.
Ps:为了简洁起见,我使用了指针"一词,但未指定其类型.
Ps: I used the word "pointer" without specify its type only for sake of brevity.
推荐答案
只要满足两个条件,此问题就可以在C中解决:
This problem has a solution in C as long as two conditions are met:
-
d
的值在编译时已知,并且 -
d
具有预定义的限制,例如10
- The value of
d
is known at compile time, and d
has a pre-defined limit, e.g. 10
您可以通过定义一系列宏并粘贴" d
的值作为标记来解决此问题:
You can solve this problem by defining a series of macros and "pasting" the value of d
as a token:
#define D_PTR(d,T) D_PTR##d(T)
#define D_PTR0(T) T
#define D_PTR1(T) T*
#define D_PTR2(T) T**
#define D_PTR3(T) T***
...
#define D_PTR10(T) T**********
现在您可以像这样声明d
维指针:
Now you can declare d
-dimension pointers like this:
D_PTR(5,int) ptr5 = NULL;
这篇关于创建三维指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!