-2

I want to create an array that has a double value at it's zeroth index, a pointer that points to a double array, and a pointer that points to an integer array. Is this possible? Or should I use a structure?

Something like this: pointer description

sigsegv
  • 159
  • 7
  • Use a pointer to `union` incorporating all of these types. Or `void*`... – Eugene Sh. Jun 27 '18 at 13:34
  • 1
    Please tell us ore about this array. What should be contained in index 1,2,3 etc.? The image you're linking to is very unclear. – Jabberwocky Jun 27 '18 at 13:35
  • 1
    It really sounds like you want a structure, not an "array". What is the *real* problem you need to solve? Why do you want an "array"? What are you supposed to do with it? – Some programmer dude Jun 27 '18 at 13:36

3 Answers3

0

It is possible to do that, with a void pointer for example.

BUT, don't go for this approach, since what you need (based on what you said), is a struct, with fields a double, a pointer to a double array, and a pointer to an integer array, like this:

struct myStruct {
   double  v;
   double* d_ptr;
   int*    i_ptr;
};
gsamaras
  • 71,951
  • 46
  • 188
  • 305
-1

Arrays are for storing a list of values of the same type, while a structs are for storing a list of values of differing types.

So what you want is a struct:

struct data {
    double val0;
    double *dbl_list;
    int *int_list;
};
dbush
  • 205,898
  • 23
  • 218
  • 273
-1

You can use a struct to make the list data structure and that struct contains a union to hold the different type of elements, for example:

typedef struct MY_LIST {
    int utype;
    union {
        int intval;
        double dblval;
    } u;
    struct MY_LIST *next;
} t_myList;

Integer utype will hold an indicator of what is stored in the union (e.g. value 1 means the integer and value 2 means the double).

You can address an element now as:

t_myList *p;
...
if (p->utype==1)
    printf("integer value %d\n", p->u.intval);
...
p= p->next;
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • But this has become a list. I want random access. – sigsegv Jun 27 '18 at 14:58
  • Your image doesn't show that. It shows pointers. However, then you can declare an array of these structs (without the `next` element). I have given you some ideas; maybe it is time to use it to make what you want yourself. – Paul Ogilvie Jun 27 '18 at 15:04