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
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
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;
};
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;
};
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;