This is a follow-up question of this: C - Convert array of elements into 2-d matrix
To do the conversion, the Method I got there is:
int (* M)[b] = (int (*) [b])array
where M[a][b]
and array[a * b]
.
The problem is that I found when I do the conversion this way, there is actually an array of size b
allocating on the stack, and it is kind of expensive for a large b
. So I would like to know if there is any way to do the conversion without such allocating.
Example (update):
/* conversion.c */
int main(void) {
int flag, array[30], b = 3;
if (flag)
goto done;
int (* M)[b] = (int (*)[b]) array;
done:
return 0;
}
compiling with gcc-9:
conversion.c: In function 'main':
conversion.c:5:3: error: jump into scope of identifier with variably modified type
5 | goto done;
| ^~~~
conversion.c:9:1: note: label 'done' defined here
9 | done:
| ^~~~
conversion.c:7:9: note: 'M' declared here
7 | int (* M)[b] = (int (*)[b]) array;
| ^
conversion.c:5:3: error: jump into scope of identifier with variably modified type
5 | goto done;
| ^~~~
conversion.c:9:1: note: label 'done' defined here
9 | done:
| ^~~~
conversion.c:7:9: note: '({anonymous})' declared here
7 | int (* M)[b] = (int (*)[b]) array;
| ^
update:
error: jump into scope of identifier with variably
tells that the goto statement is trying to jump across a (compile time)unknown size of memory on stack, which means there is memory allocated on stack due to the conversion above.