> Okay, but I have two questions. What is elemSize that you are passing into the createArray-function? Is that the size of the data to be stored in the array?
Yes it is, like so
Code:
int dimens[] = { 2, 3, 4, 5 };
myArrayADT_t *intArray = createArray(4, dimens, sizeof(int) );
myArrayADT_t *doubleArray = createArray(4, dimens, sizeof(double) );
> Isnt it possible to just have a void pointer in the structure thus making it able to store any data?
Sure you can make it a void* if you want, but at some point you need to do address arithmetic on it in order to access elements of the array.
This arithmetic is a lot easier if the pointer is char* to begin with.
> Also, will I be able to use normal array syntax on this array? You know like result[4][5][2] etc?
No.
That's why you need read/write functions taking an instance of your ADT as a parameter.
You can do this
Code:
// creates a dynamic version of result[5][6][7];
// someCreateFunction is hard-coded to always make [x][6][7] arrays.
int (*result)[6][7] = someCreateFunction(5);
and be able to do access result[4][5][2] right out of the gate, but that is very far from being abstract.
C doesn't allow you to create types at run-time, so you can't create the correct type for the dimens in the first example in order to be able to do subscripting directly.