创建三维指针 [英] Create a d-dimensional pointer

查看:184
本文介绍了创建三维指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们用来用符号*表示一个指针.重复该过程,我们获得一个双"指针**,一个三重"指针***,更一般地,一个"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆