Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding the C declaration and making use of it

I'm using a GeomagnetismLibrary and one of the function declarations have a format

int MAG_robustReadMagModels(char *filename, MAGtype_MagneticModel *(*magneticmodels)[], int array_size)

For the sake of simplicity I've dumbed it down to just focus on my goal

void blah(int *(*a)[])
{
    (*a)[0] = malloc(sizeof(int));
    (**a)[0] = 12;
}

If I want to call this function I have to declare a variable like:

int *a[1];
blah(&a);

Now in my situation no matter what a will never have more than one element, so I don't want to declare a as an array, but rather as just a pointer like

int *a;

Is there any way I can type cast or dereference this variable when calling blah that will work as desired and not cause a segfault?

Also, how would you define that type in terms of a type cast, for example: (int *[]*)?

Thanks

like image 916
lukecampbell Avatar asked Jun 27 '13 13:06

lukecampbell


1 Answers

This would do:

int * b;

blah((int *(*)[]) &b);
like image 186
alk Avatar answered Nov 15 '22 04:11

alk