views:

151

answers:

3

Hi,

I found this example code and I tried to google what int (*)[])var1 could stand for, but I got no usefull results.

#include <unistd.h>
#include <stdlib.h>

int i(int n,int m,int var1[n][m]) {
    return var1[0][0];
}

int example() {
    int *var1 = malloc(100);
    return i(10,10,(int (*)[])var1);
} 

Normally I work with VLAs in C99 so I am used to:

#include <unistd.h>
#include <stdlib.h>

int i(int n,int m,int var1[n][m]) {
    return var1[0][0];
}

int example() {
    int var1[10][10];
    return i(10,10,var1);
} 

Thanks!

+9  A: 

It means "cast var1 into pointer to array of int".

Michael Madsen
great reference site!
xtofl
I was going to suggest the `cdecl` tool, nice to see a web front-end for it :)
crazyscot
+1  A: 

(int (*)[]) is a pointer to an array of ints. Equivalent to the int[][] function argument.

This is a common idiom in C: first do a malloc to reserve memory, then cast it to the desired type.

xtofl
+1  A: 

It's a typecast to a pointer that points to an array of int.

Curd