Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a d-dimensional pointer [closed]

Tags:

c

pointers

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).

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: I used the word "pointer" without specify its type only for sake of brevity.

like image 658
Biagio Avatar asked Dec 14 '22 09:12

Biagio


1 Answers

This problem has a solution in C as long as two conditions are met:

  • The value of d is known at compile time, and
  • d has a pre-defined limit, e.g. 10

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**********

Now you can declare d-dimension pointers like this:

D_PTR(5,int) ptr5 = NULL;

Demo.

like image 59
Sergey Kalinichenko Avatar answered Dec 26 '22 00:12

Sergey Kalinichenko