简体   繁体   中英

Initialization of an array of pointers to pointers

I need to initialize an array of pointers to pointers to four variables a , b , c and d in this way:

float a = 2.8f, b = 3.7f, c = 4.6f, d = 5.5f;
float *pts[] = { &a, &b, &c, &d };
float **ptpts[4];

void main() {
    ptpts[0] = &pts[0];
    ptpts[1] = &pts[1];
    ptpts[2] = &pts[2];
    ptpts[3] = &pts[3];

    int i;
    for (i = 0; i < 4; i++) {
        printf("ptpts[%d] = %f\n", i, **ptpts[i]);
    }
}

Isn't there a simpler way to initialize the array ptpts directly at declaration using directly the array pts in order to get rid of the assignment of each element in ptpts one by one. Something like that (but doesn't work):

float *pts[] = { &a, &b, &c, &d };
float **ptpts = &pts; // => initialization from incompatible pointer type

In other words, isn't there a way to get the corresponding array of references from an array of pointers?

Edit:

Following some comments, I give here a piece of background to explain how I came to that question. This may help to propose the solution or give idea for a better architecture.

I have the following interface ( interface.h ) for the program I'm developing. Depending on a flag PTTYPE defined at compilation time, the interface will be a set of variables or a set of pointers depending on the environment where the program is deployed. The program must be compliant with both kind of interface type ( PTTYPE=1 or PTTYPE=0 ).

// interface.h 
#if PTTYPE
    float *pta, *ptb, *ptc, *ptd;
    #define a (*pta)
    #define b (*ptb)
    #define c (*ptc)
    #define d (*ptd)
#else
    float a, b, c, d;
#endif

I'm not the owner of this interface file and can't modify it. I must just include this file in my program and want to have a simple and unique way to reach the values. That's why I thought about using an array of pointers to pointers and would have something like that:

#include "interface.h"

#if PTTYPE
    float **ptpts[]={ &pta, &ptb, &ptc, &ptd };
#else
    float *pts[]={ &a, &b, &c, &d };
    float **ptpts = &pts; // => initialization from incompatible pointer
#end

void main() {
    a=2.8f;
    b=3.7f;
    c=4.6f;
    d=5.5f;

    int i;
    for (i = 0; i < 4; i++) {
        printf("ptpts[%d] = %f\n", i, **ptpts[i]);
    }
}
float ..... = &pts;

Yes you can but you need another pointer type (pointer to array):

float a = 2.8f, b = 3.7f, c = 4.6f, d = 5.5f;
float *pts[] = { &a, &b, &c, &d };
float *(*ptpts)[4] = &pts;

for (size_t i = 0; i < 4; i++) 
{
    printf("*ptpts[0][%zu] = %f\n", i, *ptpts[0][i]);
}

or

    for (size_t i = 0; i < 4; i++) {
        printf("ptpts[%zu] = %f\n", i, *(*ptpts[i]));
    }

Yes you can even without any variables

int main(void)
{
    float **pptrs[] = {(float **){&(float *){&(float){1.0f}}}, 
                    (float **){&(float *){&(float){2.0f}}}, 
                    (float **){&(float *){&(float){3.0f}}}, 
                    (float **){&(float *){&(float){4.0f}}}, };
}

The array can be defined and initialized inline this way:

float **ptpts[] = { &pts[0], &pts[1], &pts[2], &pts[3]};

If the array is global and the initization local in main , a loop or a series of assignments is required.

It's okay to use whitespace to aid readability.

int main() {
    float a=2.8f, b=3.7f, c=4.6f, d=5.5f;
    float *pts[]={ &a, &b, &c, &d };

    // What you want?
    float **ptpts[] = { &pts[0],  &pts[1], &pts[2],  &pts[3] };

    for( int i = 0; i < 4; i++ )
        printf("ptpts[%d] = %f\n", i, **ptpts[i]);

    return 0;
}

Output:

ptpts[0] = 2.800000
ptpts[1] = 3.700000
ptpts[2] = 4.600000
ptpts[3] = 5.500000

So, thanks to the help of 0___________ , I was able to find a solution which answers to my initial problem (ie initialize the array of pointers to pointer with either pointers or variable). In this way, I'm not using any more the intermediate array of pointer pts but create directly the array of pointers to pointer using the .

Here is the initialization part of the array ptpts:

#if ITYPE
#define ADAPT(name) pt##name
#else
#define ADAPT(name) (float *){&name}
#endif

float **ptpts[] = { &ADAPT(a), 
                    &ADAPT(b), 
                    &ADAPT(c), 
                    &ADAPT(d) };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM